Create 2400 virtual routes
Permalink
Hi all,
I'm struggling with the routing of 5.7. I dont know how to solve it.
What i want:
Catch virtual urls, where cityname is the virtual part: domain.com/cityname
There are around 2400 cities. The page i need to show is a pagetype or singlepage.
What i've tried / can try:
1. Calling Route::register 2400 times is not the way to go i assume. This would take a lot of time for every single call.
2. Override the page_not_found single page. Check to see if it's a proper cityname, load the pagetype, render the view, show the output and hack the response statuscode (404 -> 200).
I went for option number 2 and got stuck. What is the best way to solve this problem? Are there any other options? Is option number 2 even possible?
Gr,
Justin
I'm struggling with the routing of 5.7. I dont know how to solve it.
What i want:
Catch virtual urls, where cityname is the virtual part: domain.com/cityname
There are around 2400 cities. The page i need to show is a pagetype or singlepage.
What i've tried / can try:
1. Calling Route::register 2400 times is not the way to go i assume. This would take a lot of time for every single call.
2. Override the page_not_found single page. Check to see if it's a proper cityname, load the pagetype, render the view, show the output and hack the response statuscode (404 -> 200).
I went for option number 2 and got stuck. What is the best way to solve this problem? Are there any other options? Is option number 2 even possible?
Gr,
Justin
Just speculating...
Can you create a route to a controller method that accepts parameters? With single pages, each /path_part/ gets mapped to a declared parameter.
Don't know if that will work in your case.
Can you create a route to a controller method that accepts parameters? With single pages, each /path_part/ gets mapped to a declared parameter.
Don't know if that will work in your case.
i can, but its basically the same approach as above.
You still have to render a fake page by loading a single page and you'll end up with the header_required error which i still havent solved.
You still have to render a fake page by loading a single page and you'll end up with the header_required error which i still havent solved.
Setting the page in the Request object did the trick.
Placing the script inside a controller and connecting with a db is the next step.
Glad it works now.
Route::register('/{cityname}', function ($cityname) { global $c; if ($cityname=="test") { $c = Page::getById(278); $r = Request::getInstance(); $r->setCurrentPage($c); $page = $c->getPageController(); $page->on_start(); $page->runAction('view'); $page->set('city', $cityname); $page->on_before_render(); $view = $page->getViewObject(); print $view->render(); exit; }
Viewing 15 lines of 17 lines. View entire code block.
Placing the script inside a controller and connecting with a db is the next step.
Glad it works now.
When you have it working, perhaps you can write a howto. This is the sort of detail that is badly missing from the 5.7 docs.
I second that, please write a How-To when you find a solution.
Something like this might work:
Using a existing page (278) and render the view manually.
This is not working though. The header_required cant find the $c object and gives an error:
Call to a member function getCollectionAttributeValue() on a non-object in file /application/elements/header_required.php
How to overcome this?