Multiple URL Suffix (SOLVED - UPDATED for CI 1.6.0) |
[eluser]naruto[/eluser]
Hi, Is there any way to use multiple url suffix? Probably something like this: $config['url_suffix'] = ".html, .wsdl, .xsd"; Thus, system that consume my web service can access a dynamically generated WSDL at (for example) the following url: http://soap.mysite.com/index.php/schemas...tract.wsdl Currently I am considering to hack CodeIgniter core to do this. Or is it possible to do this by using 'hooks' ? Cheers, Naruto
[eluser]Phil Sturgeon[/eluser]
Ok my first reply was a little off, deleted, try this instead. Add in config.php: Code: $config['url_suffix'] = ".html"; Then in libraries/Router.php after line 120 Code: foreach(explode(', ', $this->config->item('allowed_suffixes')) as $suffix): Not tested but should do the trick.
[eluser]ejangi[/eluser]
I wanted to do something quite similar naruto, here's what I do. In my config/routes.php file I added this line: Code: $route['(:any)/(:any)\.(:any)'] = "$1/$2"; And then I added an "overwrite" class for the URI library. So, create a file in your "libraries" folder called MY_URI.php, with the following: Code: class MY_URI extends CI_URI { So, now in my controller methods I can do something like this: Code: if ($this->uri->format() == 'wsdl') { I hope this helps.
[eluser]naruto[/eluser]
Thanks Guys. Both solutions work really well. Only one note: URI library is loaded automatically by CodeIgniter. Cheers, Naruto
[eluser]Grahack[/eluser]
@ Pyro About the explode and for more flexibility, I prefer to explode(',', $string_to_explode); then use trim(). @ Naruto Why do you precise that URI is autoloaded, is it important for that matter? @ all This is nice!
[eluser]naruto[/eluser]
Ah sorry for not being very clear. We dont need to load the URI library since it is autoloaded by CI. If you tried to do $this->load->library('URI') PHP will report an error. This also applies to the custom MY_URI library. It will be autoloaded by CI - there is no need to load it. Yes, this is a nice solution - and I think this feature (multiple url suffix) should be included on next CI release! Cheers, Naruto
[eluser]ejangi[/eluser]
I agree naruto - being able to process different code based on the requested "format" would be great to see built into CI. Let's hope the CI God's are listening (Derek)! ![]()
[eluser]naruto[/eluser]
CodeIgniter 1.6.0 breaks this script. To fix it, replace the format() function in MY_URI.php as follow: Code: /**
[eluser]Code Arachn!d[/eluser]
for semantic reasons it may make sense to use "get_extension" as the function name instead of format - because technically you're not really getting the format just the "file extension" for the uri... that's my 2 cent - otherwise both of these are great ways to tackle this issue. |
Welcome Guest, Not a member yet? Register Sign In |