Adding routing logic in a package controller
Permalink
Hi there,
I'm building this package for someone but I'm struggling with some of the routing.
I got a table with a bunch of routes generated while creating new objects of a specific type (similar to the PagePaths table). The exception is that they are not linked to a Page. Rather, they will show information about a building placed in a certain category.
For example the route "/en/villas/villa-type-1" should show the English information about the villa type 1 in the villas category.
I'm not willing to generate pages for every created building because that could result in thousands of pages in the page tree structure. It's also a multilingual website, so that would result in to a giant mess.
Now the only problem I have is the routing. Since there is no page "/en/villas/villa-type-1" it would naturally result in to a 404 error. So I decided to add some extra routing in the package controller's on_start method. I have a few questions about this.
I am a stickler for best practices so I want to do it the 'right' way.
Is the on_start method the place where I should add new routing logic or should it be added in the /bootstrap/start.php file?
I have tested 2 options, one of which works fine but isn't using the Symfony router that C5 uses. And I'm also afraid of a performance hit while using this option:
The second option uses the router C5 uses, but isn't completely working for me.
I would prefer the second solution if it was working the way I envisioned.
The problem is that the second solution only catches "/en" but not "/en/villas/villa-type-1".
Another problem is that I don't know how to let the request bubble up when the path is not found in my table. So for example, the second solution catches the URL "/contact" which I actually a page I want to show. "/contact" is not found in my table so it should show the page, but instead it just shows a white page. So what I want is an else statement in which I tell c5 to do whatever it normally does so the page gets rendered normally.
I'm sorry for the confusing description of my problem, I still hope someone out there can give me some pointers.
I'm building this package for someone but I'm struggling with some of the routing.
I got a table with a bunch of routes generated while creating new objects of a specific type (similar to the PagePaths table). The exception is that they are not linked to a Page. Rather, they will show information about a building placed in a certain category.
For example the route "/en/villas/villa-type-1" should show the English information about the villa type 1 in the villas category.
I'm not willing to generate pages for every created building because that could result in thousands of pages in the page tree structure. It's also a multilingual website, so that would result in to a giant mess.
Now the only problem I have is the routing. Since there is no page "/en/villas/villa-type-1" it would naturally result in to a 404 error. So I decided to add some extra routing in the package controller's on_start method. I have a few questions about this.
I am a stickler for best practices so I want to do it the 'right' way.
Is the on_start method the place where I should add new routing logic or should it be added in the /bootstrap/start.php file?
I have tested 2 options, one of which works fine but isn't using the Symfony router that C5 uses. And I'm also afraid of a performance hit while using this option:
$request = Request::getInstance(); $slug = trim($request->getPathInfo(), '/'); $em = Database::get()->getEntityManager(); $obj = $em->getRepository('\Concrete\Package\PACKAGE\Src\MODEL')->findBy(array('sku' => $slug)); if($obj){ \Concrete\Package\PACKAGE\Controller\SinglePage\Dashboard\CONTROLLER::loadPage($obj[0]); }
The second option uses the router C5 uses, but isn't completely working for me.
Route::register( '{slug}', function($slug) { $em = Database::get()->getEntityManager(); $obj= $em->getRepository('\Concrete\Package\PACKAGE\Src\MODEL')->findBy(array('sku' => $slug)); if($obj){ \Concrete\Package\PACKAGE\Controller\SinglePage\Dashboard\CONTROLLER::loadPage($obj[0]); } } );
I would prefer the second solution if it was working the way I envisioned.
The problem is that the second solution only catches "/en" but not "/en/villas/villa-type-1".
Another problem is that I don't know how to let the request bubble up when the path is not found in my table. So for example, the second solution catches the URL "/contact" which I actually a page I want to show. "/contact" is not found in my table so it should show the page, but instead it just shows a white page. So what I want is an else statement in which I tell c5 to do whatever it normally does so the page gets rendered normally.
I'm sorry for the confusing description of my problem, I still hope someone out there can give me some pointers.