Welcome Guest, Not a member yet? Register   Sign In
Multiple URL Suffix (SOLVED - UPDATED for CI 1.6.0)
#1

[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
#2

[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";
$config['allowed_suffixes'] = ".wsdl, .xsd";

Then in libraries/Router.php after line 120

Code:
foreach(explode(', ', $this->config->item('allowed_suffixes')) as $suffix):
                $this->uri_string = str_replace($suffix, $this->uri_string);
endforeach;

Not tested but should do the trick.
#3

[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";
so I allow different file types to be put into the URI.

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 {
    
    
    /**
     * Constructor
     *
     * @return void
     */
    public function MY_URI()
    {
        parent::CI_URI();
    } // MY_URI()



    /**
     * Grab the requested file format fromt he URI
     * This requires an addition to the routes.php file:
     *
     * $route['(:any)/(:any)\.(:any)'] = "$1/$2";
     * @return string
     */
    public function format($default_return = false, $separator = '.')
    {
        $format = $default_return;
        if (count($this->router->segments)) {
            $last_segment = explode($separator, end($this->router->segments));
            if (count($last_segment) > 1) {
                $format = strtolower(end($last_segment));
            }
        }
        return $format;
    } // format()

} // END class MY_URI

So, now in my controller methods I can do something like this:
Code:
if ($this->uri->format() == 'wsdl') {
    // Do web service stuff
} else {
    // Normal HTML stuff.
}

I hope this helps.
#4

[eluser]naruto[/eluser]
Thanks Guys.
Both solutions work really well.

Only one note:
URI library is loaded automatically by CodeIgniter.

Cheers,
Naruto
#5

[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!
#6

[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
#7

[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)! Tongue
#8

[eluser]naruto[/eluser]
CodeIgniter 1.6.0 breaks this script. To fix it, replace the format() function in MY_URI.php as follow:
Code:
/**
* Grab the requested file format fromt he URI
* This requires an addition to the routes.php file:
*
* $route['(:any)/(:any)\.(:any)'] = "$1/$2";
* @return string
*/
public function format($default_return = false, $separator = '.')
{
    $format = $default_return;
    if (count($this->segments)) {
        $last_segment = explode($separator, end($this->segments));
        if (count($last_segment) > 1) {
            $format = strtolower(end($last_segment));
        }
    }        
    return $format;
} // format()
#9

[eluser]ejangi[/eluser]
Thanks naruto!
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB