0

What I am looking for is a way to implement MVC without needing the controller specified in the uri. The problem I am finding is that the only way I can have a simple url like example.com/blue-widget is if I first query the database to determine if the product table has the custom uri specified of "blue-widget" if it returns a row at all I can then route to the product controller. The problem with this method is that I have to query each table and then check if any rows are returned to determine which controller to route to. This feels like overkill. Especially if I end up needing more than a handful of controllers.

I would like to keep the traditional functionality of a typical MVC application so that the routing is controller/action/arguments. I simply want to add the feature so that the routing portion could determine what controller to use based on a custom-uri preferably without needing to query the database, but if that is the only option I will have to accept that and maybe minimize it to a single query that determines which table it is found in.

In short traditionally for me to access the product page of blue-widget I would need to have a url at minimum like this:

example.com/product/blue-widget

What I am looking for is a url like this:

example.com/blue-widget

but having some sort of logic to determine if the uri blue widget is a product, or a page, or a category etc without needing to create any kind of map of every possible custom uri.

This is a custom MVC implementation and not based on any particular framework.

Rudy Garcia
  • 522
  • 1
  • 6
  • 19
  • Why don't you cache the results of the database in a file, then you only need to query it once .. in theory. You would need a strategy to update them automatic like ( or just manually do it ). For example using ob_start(); ob_end_clean(); and var export you can save an array to a file, I can give a better example if you want. Personally I would use an event driven MVC router that's what I'm working on building. – ArtisticPhoenix Jul 14 '14 at 16:57
  • your looking for a `router`, since you have a custom MVC implementation you may want to look at a few on github, other then that you could name your controller 'blue-widget.php' and then check to see if the file exists. – cmorrissey Jul 14 '14 at 16:57

1 Answers1

0

There are two primary options as I see it:

  • have your router configured so that by default it matches classical /:controller/:action/:id structure, and if mo match is found, then it defaults to /:id pattern, with default values for :controller and :action segments.

  • have your router to primarily function using APCu cache, where each :id has a matching entry of controllers and action names associated with it. If the cache lookup fails, then it falls back standard pattern-matching as a backup.

Maybe this post is also useful: https://stackoverflow.com/a/19309893/727208

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150