Php – Using default parameters for 404 error (PHP with mvc design)

error messagesmvcPHP

I have a custom made Framework (written in PHP). It all works very good, but i have some doubts about a certain thing.

Right now when a user call this url for example:

http://host.com/user/edit/12

Which would resolve to:

  • user = userController
  • edit = editAction() in userController
  • 12 = treated as a param

But suppose the controller 'userController' doesn't exist. Then i could throw a 404. But on the other hand, the url could also be used as params for the indexController (which is the default controller). So in that case:

  • controller = indexController
  • user = could be an action in indexController, otherwise treated as a param
  • edit = treated as a param
  • 12 = treated as a param

That is actually how it works right now in my framework. So basically, i never throw a 404.

I could ofcourse say that only params can be given if the controller name is explicitly named in the URL. So if i want the above url:

http://host.com/user/edit/12

To be invoked by the indexController, in the indexAction. Then i specifically have to tell what controller and action it uses in the URL.

So the URL should become:

http://host.com/index/index/user/edit/12
  • index = indexController
  • index (2nd one) = the action method
  • user = treated as a param
  • edit = treated as a param
  • 12 = treated as a param

That way, when a controller doesn't exist, i don't reroute everything as a param to the index controller and simply throw a 404 error.

Now my question is, which one is more preffered? Should i allow both options to be configurable in a config file? Or should i always use one of them. Simply because that's the only and best way to do it?

Best Answer

If you are forcing the system to call the url http://host.com/user/edit/12 like http://host.com/index/index/user/edit/12 then you are rerouting for no reason for every controller that exists and you logic would only be good for a non-existing controller.

That way, when a controller doesn't exist, i don't reroute everything as a param to the index controller and simply throw a 404 error.

besides this if you have finally decided to throw 404 then why not do that in the main executing file? probably index.php?

Your system in index.php checks for a controller if exists execute it else pass it to default controller that checks if the said params contain any action for default controller and if it exists then fine else shows 404 error. Why involving default controller for this you are ultimately calling default controller for no reason.

Related Topic