Welcome Guest, Not a member yet? Register   Sign In
how to implement dynamic "routes"?
#1

[eluser]narkaT[/eluser]
Hi fellas,

I'm planing to use CI for my next project, and I'm currently researching,
if its possible to implement all the required functionality.

So far I've got following URL-scheme working when browsing the Site with an browser:
Quote:http://testserver/controller/function.ht...country=de

The problem I'm encountering now is, that I need to dynamically map aliases to URI's.
Mappings have to be editable/deletable and so on...

Mapping Example:
http://testserver/asd.html
maps to:
http://testserver/controller/function.ht...country=de


how do I implement such functionality with CI?
the routing-class obviously doesn't support "passing" GET-Parameters.

I'm open to any alternatives Smile


Greetings
Jan
#2

[eluser]Mirage[/eluser]
Well you won't be able to route to a page using query parameters.

Your destination uri would should look something like this:

Code:
http://testserver/controller/function/asd/de

Making it dynamic is easy enough, simply put some code in the router config that builds the routes from your datasource. Editing it requires that you expose a front-end to the datasource. That should do it.

HTH
-m
#3

[eluser]narkaT[/eluser]
that was my initial aproach Wink

but I encountered several problems:

1. utf-8 chars aren't allowed in the URI. Adding them to the permitted chars fails,
because the 'permitted_uri_chars'-config value is escaped by preg_quote
which turns "\x81-\xFF" to "\\x81-\\xFF".

2. passing "arrays" of values is not possible without a custom implantation,
which parses the URI.

(3. the total number of parameters is "unpredictable", so statically mapping
segment x to value y won't work)

Problem 3 could be eliminated with the $this->uri->uri_to_assoc()-method,
but problem 2 would still consist.


Problem 2. exists because there will be multi-select-fields on the page,
which will transform to a parameter-string like "select[]=1select[]=2".


One solution could be usring post for the form, and convert the post data
to a valid CI-URI, but that would be a little bit "hacky" to implement.
encode/decode text-data that my contain utf-8-chars and parse the "arrays"
back to an array and so on...

the site will be "good visited", so every millisecond/byte counts Wink
if it isn't possible on any other way I'll have to stick with that solution.

edit: Just read through the URI-Class, I'm going to resume my Hook-Aproach.


Greeting
Jan
#4

[eluser]Mirage[/eluser]
If you allow query strings, they can contain utf-8 chars no? Your controller and method can probably do without utf-8 codes...

So forget about routing the query string to uri parameters. Simply create routes that get you to the proper controller/method. Evaluating input is the job of the controller, so you can access your $_GET or $_POST in the controller and process the request as usual.

HTH,
-m
#5

[eluser]narkaT[/eluser]
I would absolutely agree with you, if there weren't these specific requirements
that need to be fulfilled Wink

There will be a complex search-mask, and every result-set this search returns
has to be 1) linkable/bookmarkable 2) mappable to a simple uri.

for exmaple "phrase.html" -> "/myclass/medthod.html?search=phrase&.....many many more params"

thats why routing the methods and appending the parameters per get-aparams,
won't fulfill the required features.



I found a interesting thread.

Okay, until now the only problem I couldn't get around is the database connection in the
pre_system-hook :roll:
because the base-class isn't included in that state of execution, the get_instance function
used in the database-method of the Loader-class isn't available.

Including the base-class falls out of the lists of options.
It gets included in the file Codeigniter.php and would cause an error.

any suggestions for a "beginner"? Wink

edit: reading code helps Wink absurd "problems" appears without lunch at lunch time... Smile


edit2: if anyone is interested in the final code:
Code:
class DBRoutes {

    function find() {
        $URI =& load_class('URI');
        $URI->_fetch_uri_string();
        $URI->_remove_url_suffix();
        $URI->_explode_segments();

        if ( count($URI->segments) && !file_exists(APPPATH.'controllers/'.$URI->segments[0].EXT) ) {
            //real controller does not exists -> lookup
            require_once(BASEPATH.'database/DB'.EXT);
            $db = DB('', true);
            $query = $db->select('route')->from('seo_routes')->where(array('alias' => substr($_SERVER['PATH_INFO'], 1)))->limit(1)->get();

            if ( $query->num_rows ) {
                $data = $query->result();
                $data = explode('?', $data[0]->route);

                if ( isset($data[1]) ) {
                    parse_str($data[1], $get);
                    $_GET = array_merge($_GET, $get);
                    $_SERVER['QUERY_STRING'] = $_SERVER['QUERY_STRING'] .'&'. $data[1];
                }
                $_SERVER['PATH_INFO'] = '/'. $data[0];

                $URI->segments = array();
                $URI->_fetch_uri_string();
                $URI->_remove_url_suffix();
                $URI->_explode_segments();
            }
        }
    }
}
the uri_protocol setting is explicit set to PATH_INFO.
the execution time of 0.03x s isn't that lighting fast, but its easy to maintain and highly flexible.

Greetings
Jan
#6

[eluser]Jamie Rumbelow[/eluser]
It's CodeIgniter convention to pass most values over POST, rather then GET.

Doing so will eliminate your problem completely.
#7

[eluser]narkaT[/eluser]
but post values aren't "copyable", and creating such "aliases"
should be as easy as it could.

copy an URI and assign it to an alias.

The users that'll use the application don't even know that such
things like "get" and "post" exists.
so I'm trying to keep it as simple as possible to create aliases Wink

the code I posted does the job so far Smile

Greetings
Jan
#8

[eluser]Bramme[/eluser]
Just something that popped in my head: you could process your search form through $_POST, shedding your uri problems, but store the search variables in a database table. You can then make previous searches linkable/bookmarkable by simply mentioning the search ID in your uri...

Just a thought though, don't know if you could use it.
#9

[eluser]narkaT[/eluser]
That's an interesting idea.
I'm gonna think about that Wink

Greetings
Jan
#10

[eluser]drewbee[/eluser]
Bramme hit it on the head, infact, that is the way the search works on these forums.

This can also be used to cache search results. Smile




Theme © iAndrew 2016 - Forum software by © MyBB