Welcome Guest, Not a member yet? Register   Sign In
Default URI Routing Behaviour
#1

[eluser]deand[/eluser]
Hi all,

I want to "disable" the implicit routing behaviour (i.e. www.your-site.com/class/function/ID) and the access to the controllers/methods to be only by explicit URI routing and no other way.

For example, if I have "welcome" to be the default controller I want it to be accessible only at www.your-site.com/ and not at www.your-site.com/welcome. The same goes for the other controllers.

Does anyone have an idea how it can be achieved?

In Symfony this default behaviour is made as a pre-configured routing rule (the last one) which can be changed easily. Is it possible something similar in CodeIgniter?

All suggestions are welcome. Thanks in advance!
#2

[eluser]Michael Wales[/eluser]
Why?

I dunno, it seems like most people's answer to this is is 'just because' - which isn't good enough.
#3

[eluser]coolfactor[/eluser]
Investigate the _remap() feature, which is called if you define it in your controller. This lets you handle your routing in your controller. Of course, you still need to invoke the correct controller, so can you give us some examples of your explicit routes? That would really help.
#4

[eluser]deand[/eluser]
Why I want it that way?

Because it gives absolute control which for me, in the case, relates to simplicity.

I'm not saying "Change the way it all works!" - just want to know "how". I'm not familiar enough with the whole architecture and that's why I'm asking.
#5

[eluser]deand[/eluser]
coolfactor,

It's about the step where a controller is chosen by the router.

Apart from the default controller example, if I have mapped by $route www.your-site.com/about_us/feedback/ to controller "feedback", method "index" it will be available at www.your-site.com/feedback/ as well.
#6

[eluser]deand[/eluser]
Another example Smile

A multilingual site with URIs like http://example.com/{language}/page/. We have a controller "products" and it is intended to be accessed at http://example.com/en/products/ and http://example.com/it/products/. But it will be available at http://example.com/products/ too, because of its name.

If a user tries by memory and goes to http://example.com/products our controller matches. Now we have to add to the controller the ability to choose the language by itself or let the user choose, etc. etc.

In an ideal case such a situation should be covered, I agree. But that's rarely the case because of external (not technical) circumstances and it is better to return 404, for example.
#7

[eluser]John_Betong[/eluser]
Hi deand,

This could possibly be a solution/kludge to solve the problem:

Put this line of code at the very top of each controller and observe the results.

Code:
$calling_uri = $_SERVER['REQUEST_URI'];
  echo '<h3>REQUEST_URI ==> '  .$calling_uri .'</h3>';
  die;

I have tested this briefly and the results are different depending on how the controller was called. All you have to do is to test the result and if it is not what you want then call your '404'.

Cheers,

John_Betong
#8

[eluser]deand[/eluser]
Yes, John_Betong.

This can be a solution. As well as adding a more complex logic which will give even better results, but is something I was trying to avoid. Thank you anyway Smile.

About the _remap(), it can be a workaround, but as you said coolfactor, only for the method. Thank you for the suggestion. Wink

Another idea is to use query strings and rewrite rules in .htaccess, but it is too much going back to the basics.

As far as I understand, this default behaviour I am talking about is incorporated in _compile_segments() in Router.php. At the end of _parse_routes() (i.e. no configured route, scaffolding trigger or default route matched) it is called with the segments extracted from the request URI. Than _compile_segments() with the help of _validate_segments() tries to find a controller/method.

A question to the CodeIgniter developers: Is it a performance problem if the last try to find a matching controller/method (last call of _compile_segments()) is a pre-configured route in $route instead of hard coded logic? Or why it isn't good to implement it that way?
#9

[eluser]deand[/eluser]
To clarify what I mean here is an example of the new routes.php (/system/application) with rules implementing the default one-to-one relationship between URL string and its corresponding controller class/method:

Code:
| routes must come before any wildcard or regular expression routes.
|
*/

$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

// your application specific routes are here

$route['(:any)/(:any)'] = "$1/$2";
$route['(:any)'] = "$1/index";

?&gt;

Of course, _parse_routes() and _set_request() (as in /system/libraries/Router.php on SVN) must be changed a bit to handle this, the default controller and scaffolding trigger.

My question to the core team remains. What do you think about this approach? Do you think it is worth to make the required changes for the enhanced control?




Theme © iAndrew 2016 - Forum software by © MyBB