how to implement dynamic "routes"? |
[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 Greetings Jan
[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
[eluser]narkaT[/eluser]
that was my initial aproach 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 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
[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
[eluser]narkaT[/eluser]
I would absolutely agree with you, if there weren't these specific requirements that need to be fulfilled 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"? edit: reading code helps absurd "problems" appears without lunch at lunch time... edit2: if anyone is interested in the final code: Code: class DBRoutes { the execution time of 0.03x s isn't that lighting fast, but its easy to maintain and highly flexible. Greetings Jan
[eluser]Jamie Rumbelow[/eluser]
It's CodeIgniter convention to pass most values over POST, rather then GET. Doing so will eliminate your problem completely.
[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 the code I posted does the job so far Greetings Jan
[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.
[eluser]narkaT[/eluser]
That's an interesting idea. I'm gonna think about that Greetings Jan
[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. |
Welcome Guest, Not a member yet? Register Sign In |