Welcome Guest, Not a member yet? Register   Sign In
Getting the requested "format" from a route
#1

[eluser]ejangi[/eluser]
Hey guys,

I'm wanting to get the "format" from the route. By that I mean that from uri's like:
Code:
http://www.mydomain.com/controller/model/index.html
http://www.mydomain.com/controller/model/index.xml

I want to know whether the user is requesting HTML or XML.

Looking through the Router.php class I noticed that the "/" [read: slash] is hard-coded as the segment separator, so it looks like I will have to overwrite at least the _set_route_mapping() or _get_uri_string() methods in my own library.

I just thought that before I dive in head-first, I'd ask to see if anyone's already done this?
#2

[eluser]pixagrafik[/eluser]
I'd like to know the answer to that too. I suppose it wouldn't be difficult to plumb this in. I'm guessing that the router would need to strip the extension off of the requested URL and then set that as a variable somewhere
where your controller/models/etc would have access to it so it could do the right thing informatting the data. Basically, this is overriding the display or view, to instead call normal HTML view, it would call some type of XML view formatter.
#3

[eluser]Michael Wales[/eluser]
Code:
$format = substr($this->uri->string(), strpos($this->uri->string(), '.'), len($this->uri->string()) - strpos($this->uri->string()));

This should return html or xml from the submitted URI. It may include the period, in which case you can just add 1 to the second substr() parameter (where it uses strpos()).
#4

[eluser]ejangi[/eluser]
So I actually JUST got to this before reading the responses to this post. This is what I ended up putting in /libraries/MY_URI.php:
Code:
class MY_URI extends CI_URI {
    
    function MY_URI()
    {
        parent::CI_URI();
    }
    
    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;
    }
    
}

And in any controller I can now call:
Code:
echo $this->uri->format();
#5

[eluser]pixagrafik[/eluser]
Nice, but didn't you have to modify something else to allow the .format to not throw a 404 when appended to your URLs?
#6

[eluser]ejangi[/eluser]
No, the Router class appears to strip off the end (.html/.xml) if there is one before routing to the Controller->method(); so it just ignores the format even if you have one in the URI.
#7

[eluser]pixagrafik[/eluser]
Strange. I must have something configured incorrectly as as soon as I append a .format to any of my URLS, I get a 404.
#8

[eluser]ejangi[/eluser]
My .htaccess has this in it:

Code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Maybe that's the reason it's working???
#9

[eluser]ejangi[/eluser]
Okay, now for some reason I'm getting that same error...

Let me revisit this...

[EDIT:]

I have added a new $route[] to the routes file and it all works now (as an example):
Code:
$route['welcome/(:any)\.(:any)'] = "welcome/$1";
#10

[eluser]pixagrafik[/eluser]
Thanks! Works great. I knew there had to be more to it than just that uri object method. Great work. Now, to devise a way to do some type of automated output method switching based on the .format such that by default, it renders html, but if .json is called, for example, it a default ouput function is called and the previously set up view arguments are set up.
I suppose it might even be possible to use templates for this, much like views. A post_controller hook might be used to perform the output object translation and formatting. Anything is possible with software!




Theme © iAndrew 2016 - Forum software by © MyBB