How could I able to create, dynamic route from the database. I use the custom router, however, I could not able to integrate the doctrinaire with my custom router. The main difficulty is that, Factory is not able to connect with the custom route class. Does anyone know how to connect route with the database in ZF3?
0
votes
1 Answers
0
votes
you need segment route, eg.
'blogpost' => [
'type' => Segment::class,
'options' => [
'route' => '/novost/:id',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'blogPost',
],
],
],
notice
:id
id is dynamic variable in route
you retrive it inside action like
$id = $this->params()->fromRoute('id');
so you can get data from db for this route and serve it to ViewModel . eg:
$post = $this->model->getBlogPost($id);
$vm = new ViewModel(['post' => $post]);
return $vm;
notice, you can also have optional variables in route with brackets -> [/:variable]
When building link with url helper don't forget variables.
$this->url('blogpost',['id'=>$someid]);
more info: https://docs.zendframework.com/tutorials/in-depth-guide/understanding-routing/#segment-routes