I am trying to set two different routes to handle pagination of data, for instance:
'error' => [
'type' => 'Segment',
'options' => [
'route' => '/error[/:action][/:id][page/:page]',
'constraints' => [
'page' => '[0-9]*',
'id' => '[0-9]*',
],
'defaults' => [
'controller' => 'Admin\Controller\Error',
'action' => 'index',
],
],
],
'paginator' => [
'type' => 'Segment',
'options' => [
'route' => '/blog/list-entries/[page/:page]',
'constraints' => [
'page' => '[0-9]*',
],
'defaults' => [
'controller' => 'Admin\Controller\Blog',
'action' => 'list-entries',
],
],
],
In the error route, the paginator works for the first set of data, but when I try to go to the next page to view more data it uses /blog/list-entries/page/2 instead of the /error/[/:action][/:id][page/:page] route. Is the paginator route becoming the default route for more data?
Any help would be appreciated.
Thanks
Fixed the issue, I was passing the wrong paginator route in the view. Thanks!
Related
I'm using ZF3, in module.config.php file in Post module, I have one of these two routes,
'create-post' => [
'type' => Literal::class,
'options' => [
// Change this to something specific to your module
'route' => '/post/create',
'defaults' => [
'controller' => Controller\PostController::class,
'action' => 'create',
]
],
'may_terminate' => true,
'child_routes' => [
// You can place additional routes that match under the
// route defined above here.
],
],
'post' => [
'type' => Segment::class,
'options' => [
// Change this to something specific to your module
'route' => '/post[/:postId]',
'defaults' => [
'controller' => Controller\PostController::class,
'action' => 'show',
],
'constraints' => array(
'postId' => '\d{4}'
)
],
'may_terminate' => true,
'child_routes' => [
// You can place additional routes that match under the
// route defined above here.
],
]
Now when I visit http://localhost:8080/post/create it works, but when I visit http://localhost:8080/post/32, it doesn't work. It say 404 error, page not found.
Any help is much appreciated.
As per #jon Stirling comment on my question, I changed the constraints on post route and it worked.
Changed 'postId' => '\d{4}' to 'postId' => '\d{1,4}'
'post' => [
'type' => Segment::class,
'options' => [
// Change this to something specific to your module
'route' => '/post[/:postId]',
'defaults' => [
'controller' => Controller\PostController::class,
'action' => 'show',
],
'constraints' => array(
'postId' => '\d{1,4}'
)
],
'may_terminate' => true,
'child_routes' => [
// You can place additional routes that match under the
// route defined above here.
],
]
In Zend framework 3, I tried adding new controller "ArticleController" to a existed module City but failed. I post screen shot, my folder structure and module.config.php. Could you explain what the problem is? Incidentally, it worked when accessing http://0.0.0.0:7000/city
When accessing http://0.0.0.0:7000/article
Next, module\city\config\module.config.php codes are following:
<?php
namespace City;
use Zend\Router\Http\Segment;
return [
'router' => [
'routes' => [
'city' => [
'type' => Segment::class,
'options' => [
'route' => '/city[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\CityController::class,
'action' => 'index',
],
],
],
'article' => [
'type' => Segment::class,
'options' => [
'route' => '/article[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\ArticleController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'city' => __DIR__ . '/../view',
],
],
];
Error message is clear. Application doesn't know anything about your controller. Your module config has to have information about controllers under "controllers" key. Checkout zend documentation, you'll see "controllers" key in config file.
In ZF3 I want to get default parameter from route. I'm getting parameters in this way in controller:
$params = $this->params()->fromRoute('crud');
My urls looks like this:
1: somedomain/admin/color/add
2: somedomain/admin/color
In 1) I'm getting add in my $params variable.
In 2) I'm getting null but I'm expecting default (in this case view)
I think this is problem with bad router configuration.
'admin' => [
'type' => Segment::class,
'options' => [
'route' => '/admin/:action',
'defaults' => [
'controller' => Controller\AdminController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'color' => [
'type' => Segment::class,
'options' => [
'route' => '/:crud',
'constraints' => [
'crud' => 'add|edit|delete|view',
],
'defaults' => [
'controller' => Controller\AdminController::class,
'crud' => 'view',
],
],
],
],
],
In your route definition, you didn't says the router that your crud parameter is optionnal. So when you call somedomain/admin/color, it is the route /admin/:action which is selected.
To specify a optional parameter, use the bracket notation (assuming you use the same action):
'admin' => [
'type' => Segment::class,
'options' => [
'route' => '/admin/:action[/:crud]',
'defaults' => [
'controller' => Controller\AdminController::class,
'action' => 'index',
'crud' => 'view',
],
'constraints' => [
'crud' => 'add|edit|delete|view',
],
],
],
I want my url route to have a dynamic part and to end up on the same page not matter if whatever I have in the middle part of my URL.
E.g.:
/en/the-old-category/the-old-name/pid/123123123
/en/the-new-category/now-in-a-sub-category/the-new-name/pid/123123123
Should both get caught by the same controller/action (which will issue the pertinent 301/302 if necessary).
My current router contains:
'router' => [
'routes' => [
'blog' => [
'type' => 'segment',
'options' => [
'route' => "/[:language]",
'constraints' => [
'language' => '[a-z]{2}'
],
'defaults' => [
'controller' => 'Blog\Controller\List',
'action' => 'index',
'language' => 'en'
],
'may_terminate' => true,
'child_routes' => [
'detail' => 'segment',
'options' => [
'route' => '/:path/pid/:postid',
'constraints' => [
'path' => '.+',
'postid' => '\d{1,10}'
],
'defaults' => [
'controller' => 'Blog\Controller\List',
'action' => 'detail'
]
]
]
]
]
]
]
But it's not working.
/ and /en are being caught properly, but subroutes like the ones I proposed earlier, are not.
Am I in the right path to do what I want to do? Should I write a regex route instead?
.+ won't match / because segment routes split the path on / before applying constraints. To make your route work you'll need something like /:path/:foo/pid/:postid. A regex route might also work.
Since between the first and the last part of my URL I need to have a variable number of segments, I can't do this with a segment route, but I was able to manage it with a regex route, configured as follows:
'router' => [
'routes' => [
'blog' => [
'type' => 'regex',
'options' => [
'regex' => "/(?<language>[a-z]{2})?",
'spec' => "/%language%",
'defaults' => [
'controller' => 'Blog\Controller\List',
'action' => 'index'
],
'may_terminate' => true,
],
'child_routes' => [
'detail' => [
'type' => 'regex',
'options' => [
'regex' => '/(?<path>.+)/pid/(?<postid>\d+)$',
'spec' => '/%path%/pid/%postid%',
'defaults' => [
'controller' => 'Blog\Controller\List',
'action' => 'detail'
]
]
]
]
]
]
]
I am working in Zend Framework 2 and have variable actions and primary keys. Below is my modules router. I would like to pass different ID's to it based on the action (If I delete the language it will reference a languageId if I delete an album it will reference an albumId). Do I need to create child routes for language and album and then identify the specific ID used for each child route? Or is there a way where I can say the second parameter will always be a number and the action will do what it needs to with that number?
Action: deleteLanguage ID: languageId
ACtion: deleteAlbum ID: albumId
'router' => array(
'routes' => array(
'pro' => array(
'type' => 'segment',
'options' => array(
'route' => '/pro[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'language_id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Pro\Controller\Pro',
'action' => 'index',
),
Many Thanks,
M
You only can create a dependency between the action and the identifier if you write down the configuration completely. ZF2 has no method to rename parameter names based on other parameter values (and if you think about it, there are plenty good reasons not to have this feature).
You are left with two options:
'pro' => [
'type' => 'literal',
'options' => [
'route' => '/pro',
'defaults' => [
'controller' => 'Pro\Controller\Pro',
'action' => 'index',
],
'may_terminate' => true,
'child_routes' => [
'delete-language' => [
'type' => 'segment',
'options' => [
'route' => '/delete-language/:language_id',
'defaults' => [
'action' => 'deleteLanguage'
],
'constraints' => [
'language_id' => '[0-9]+'
],
],
],
'delete-album' => [
'type' => 'segment',
'options' => [
'route' => '/delete-album/:album_id',
'defaults' => [
'action' => 'deleteAlbum'
],
'constraints' => [
'album_id' => '[0-9]+'
],
],
],
],
],
]
Or accept you deal with an "id" only (which is much more preferred imho):
'pro' => [
'type' => 'segment',
'options' => [
'route' => '/pro[/:action[/:id]]',
'defaults' => [
'controller' => 'Pro\Controller\Pro',
'action' => 'index',
],
'constraints' => [
'id' => '[0-9]+'
],
],
]
Also, it helps a lot if you split your controllers per entity / domain model. Don't put a delete for a "language" and a delete for an "album" in the same controller. Split them! Think more in RESTful principles and it will keep your code much cleaner.