Reputation: 675
I am trying to route to the edit action of my controller but I have not been successful. Below is the content of my module.config.php file. I am working with Zend framework 2. Do I need to route every action in my controller?
<?php
return array(
'controllers' => array(
'invokables' => array(
'Manager\Controller\Index' => 'Manager\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'manager' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/manager',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Manager\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'Manager' => __DIR__ . '/../view',
),
),
);
Upvotes: 1
Views: 436
Reputation: 675
Thanks guys, I finally got it to work. My code in module.config.php is exactly like what is provided in Finbarr's answer. Here it is again.
namespace Manager;
return array(
'controllers' => array(
'invokables' => array(
'Manager\Controller\Manager' => 'Manager\Controller\ManagerController',
),
),
'router' => array(
'routes' => array(
'manager' => array(
'type' => 'segment',
'options' => array(
'route' => '/manager[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Manager\Controller\Manager',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'manager' => DIR . '/../view',
),
),
);
Upvotes: 0
Reputation: 16455
The way you set up your Route is the following:
/manager[/:controller[/:action]]
This means, the url you would look for probably is:
/manager/manager/edit
You need to understand that a child-route is nothing more but an appendix to the current route definitions. So what you could do is what @Finbarr suggested or you go with nested routes who are a little faster and would look like this:
'manager' => array(
'type' => 'Zend\Mvc\Http\Literal',
'options' => array(
'route' => '/manager',
'defaults' => array(
'controller' => 'Manager\Controller\Index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'add' => array(
'type' => 'Zend\Mvc\Http\Literal',
'options' => array(
'route' => '/add'
),
'defaults' => array(
'action' => 'add',
),
),
'edit' => array(
'type' => 'Zend\Mvc\Http\Segment',
'options' => array(
'route' => '/edit[/:id]',
'defaults' => array(
'action' => 'edit',
),
'constraints' => array(
'id' => '\d+',
),
)
),
)
)
Notice the difference in what is done. Both the routes manager
and add
are of type Literal
. This means that those urls never have any parameter attached. They can always have QueryParameters
, but RouteParameters
won't work on them.
And then you get your edit
-Route. This one now is of type Segment
, meaning that there is a part, a segment, that is variable. Furthermore the route is prepended by /edit
, so the full url now becomes at least /manager/edit
with the optional id /manager/edit[/1234]
. It is up to you to have the ID optional or not. If you don't want it optional, simply remove the brackets around it and create the route definition like this: 'route' => '/edit/:id'
, though personally i like having an 'edit-index' if no ID is attached.
Upvotes: 0
Reputation: 486
Actions can be routed in your controller with [/:action] as part of the route. This will get mapped to a function in the Controller matching the same name, eg in your example ../manager/foo will be mapped to the fooAction function, ..manager/index will be mapped to to the indexAction function etc
Usually (at least in the examples I have seen) the ID of the record to edit is passed as a parameter in the url. eg. example.com/manager/edit/1
If this is what you want to achieve you are missing a piece from your route "[/:id]" (see code below)
Try this as a simplified version for your router (module.config.php)
<?php
namespace Manager;
return array(
'controllers' => array(
'invokables' => array(
'Manager\Controller\Index' => 'Manager\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'manager' => array(
'type' => 'segment',
'options' => array(
'route' => '/manager[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Manager\Controller\Index',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'Manager' => __DIR__ . '/../view',
),
),
);
Also make sure you have an edit function in your IndexController Class "IndexController.php" eg
public function editAction()
{
//Some code to edit the record....
}
and an "edit.phtml" file in your view directory
Upvotes: 0
Reputation: 7223
Assuming you are new, there are few things you need to know if you are working on application for some CRUD, i will recommend that you must create a new module, as guided in ZF2 Skeleton app or Album app, Settings that you provided are correct and have nothing wrong.
Lets assume you already created a new module and you are working on new module, and all your routes are defined properly what you must do is include name of your Module to application.config.php file, this file is under config folder, If you already created a bew module then add its name in this section
'modules' => array(
'ZendDeveloperTools',
'DoctrineModule',
'DoctrineORMModule',
'Application,
'newlyCreatedModule'
),
Above is a part of snipppet from 1 of my files so dont be confused with it. For more refrence try to follow Album Application for ZF2. Also you dont need to write routes in any controller its handled by ZF2, in module.config.php file which we have to provide for every module.
Upvotes: 1