Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
So the URL should become: http://host.com/index/index/user/edit/12 index = indexController index (2nd one) = the action method edit = treated as a param 12 = treated as a param where is user –  Junaid Jun 27 '12 at 12:14
    
@Junaid Just missed that. I correct it. The question is simply: "If no controller is found, everything simply gets passed to the indexController as params instead of throwing a 404. Is that a bad choice, and why?" –  user1175327 Jun 27 '12 at 12:25
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.