Is there a way to access a url that is different to the controller name?
Say, for my real life example, that I want to access this url http://www.example.com/men with men being a category. My controller would be CategoryController and it would display information from a database that would be appropriate to whatever category had been selected.
The reason I need it like this is because I need to support multiple sites so I can't have a controller for each category. Is there a way to do this? I'm fairly new to the 2 Framework but have decent knowledge of 1.12. Thanks.
If I undestand you correctly, you may resolve your task by routing configuration.
Module's module.config.php file:
return array(
'router' => array(
'routes' => array(
'<ROUTE_NAME>' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:category',
'constraints' => array(
'category' => '<CATEGORY_PATTERN>', // usually [a-zA-Z0-9-_]+
),
'defaults' => array(
'__NAMESPACE__' => '<CategoryController Namespace>',
'controller' => 'Category',
'action' => '<Controller method without "action" postfix>',
),
),
),
),
),
);
Inside controller category accessed by:
$category = $this->params()->fromRoute('category');
Related
I'm trying to use a hierarchical resource in ZF2 for a Restful API. The resource should looks like clients/1/addresses. What I've tried was this
'clients' => array(
'type' => 'segment',
'options' => array(
'route' => '/clients[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Api\Controller\ClientController',
),
),
'may_terminate' => true,
'child_routes' => array(
'addresses' => array(
'type' => 'segment',
'options' => array(
'route' => '/addresses[/:address_id]',
'constraints' => array(
'address_id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Api\Controller\AddressController',
),
),
),
),
),
There is this conflict of both id's, but I don't know if I rename the route identifier id of the resource addresses like I did will solve it. Anyway, the real problem is that the route clients/1/addresses calls the get method of the AddressController, not the getList, and I think that's because Zend understands that the id of the client belongs to addresses, so its calls the get method.
Do you know how to deal with this?
You are probably right that get is called instead of getList because of the id being present in your route match parameters and the controller by default uses 'id' for matching the route identifier.
The way to deal with this is that you give the route identifiers names that fit the resource. So for client you make client_id and for address you use address_id (like you already did).
And then you configure your AbstractRestfulController instance to "look" for the correct route identifier using the setIdentifierName method:
$clientController->setIdentifierName( 'client_id' );
$addressController->setIdentifierName( 'address_id' );
This is just an example, the best way to do this is (of course) by using a controller factory...
I'm just starting off with Zend Framework, and I'm not quite sure what I'm doing wrong with the URI routing.
I'm starting with an initial Zend Framework project in Zend Studio based in my htdocs folder (I'm using Zend Server as well on Windows 7). Everything up to there seems to be working fine getting the index page up (it's running out of the /public/ subdirectory).
But when I try to add a module though, in this case called Users with a controller called Index, and following the instructions in getting that configured, I'm not sure what I should be putting in the URI to get it to route to it's view. I've tried just about every configuration of URI combinations that I can think of (localhost:80/public/users, localhost:80/public/users/index, localhost:80/users, etc)
I'm not getting a routing error, but just a plain 404 page.
Do I need to set the public folder as the root? Or is there something else I need to do to get the routing to work?
~edit in response to bitWorking
It looks like it does automatically add it to the application.config.php. But here is the module.config.php of the Users module
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/index',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Users\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(
),
),
),
),
),
),
),
Now I do see where it's guiding you to customize the routes. I've experimented with this as well, but still am not sure what I should set them to. Much closer though.
If you want to call the Index controller in your Users module with /users you have to name the route accordingly:
...
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
---------
...
Else please control the application.config.php. It should look like:
return array(
'modules' => array(
'Application',
'Users',
),
...
So the Url's should look like:
localhost/public/users -> Users/Controller/IndexController/indexAction
localhost/public/users/foo -> Users/Controller/FooController/indexAction
localhost/public/users/foo/bar -> Users/Controller/FooController/barAction
Hi I have create a route like this
'webb' => array(
'type' => 'Segment',
'options' => array(
'route' => '/oferta/[:url1[/:url2][/:url3][/:url4]]',
'defaults' => array(
'controller' => 'webb',
'action' => 'index',
),
),
So my route is looking for paremeters url1 url2 etc
I have created navigation like this (just one item to show)
'page-3' => array(
'label' => 'example',
'route' => 'webb',
'controller' => 'index',
'params' => array('url1' => aa', 'url2' => bb', 'url3' => 'cc'),
)
and when I am rendering it using zend navigation helper everything is working fine despite that the current class of item is not changing to active? Any ideas? could it be done this way or when I am passing params zend navigation will not change class?
Bur url is bulid fine i get something like this /oferta/aa/bb/cc
Please help
Remove controller key from navigation array and it should work ('controller' => 'index' is wrong anyway).
From zend.navigation.pages.mvc
Note that when using the route property in a page, you do not need to specify the default params that the route defines (controller, action, etc.).
I implemented a form for placing new orders in Zend Framework 2 and after submitting the form I should redirect to another route and take the orders.id variable in another controller.
I tried using $this->redirect()->toRoute('confirm', array('param'=>$orderId)); but it is not working at all.
Maybe I do not know how to get that parameter in another confirmAction controller.
Please give me some examples. Thank you very much.
1) Since this is a routing issue, show what you have for the route in the module.config.php file. You might not have the "param" constraint configured properly in your config if I had to guess.
It should look something like this:
'confirm' => array(
'type' => 'segment',
'options' => array(
'route' => '/controller_name/confirm[/][:param][/]',
'constraints' => array(
'param' => '[0-9]*'
),
'defaults' => array(
'__NAMESPACE__' => 'your_namespace', // ex. Application\Controller
'action' => 'confirm', // or whatever action you're calling
'controller' => 'controller_name' // ex.
),
),
),
I added zfcUser module to my project via Composer and overrided it in the module ZfcUserOverride. I want trailing slash work, so I added route in overrided module.
zfcUserOverride file module.config.php contents below:
<?php
$config = array(
'view_manager' => array(
'template_path_stack' => array(
'zfcuser' => __DIR__ . '/../view',
),
),
'controllers' => array(
'invokables' => array(
'zfcuser' => 'ZfcUserOverride\Controller\UserController',
),
)
);
$config['router']['routes']['zfcuser']['child_routes']['trailing_slash'] = array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'index',
),
),
);
return $config;
I added new path, everythin is working correct.
But what if I want remove route? How to do this? I need somethink like:
$config['router']['routes']['zfcuser']['child_routes']['login'] = null;
Help please. Thank you.
In zfcUserOverride you will need to override the route config rather than add a new one.
This can easily be done by using the same array key when defining the routes.
For example; should I wish to modify the login route to allow the extra slash I would use this:
// zfcUserOverride/config/module.config.php
'router' => array(
'routes' => array(
'zfcuser' => array(
'child_routes' => array(
'login' => array(
'type' => 'Segment',
'options' => array(
'route' => '/login[/]',
),
),
),
),
),
);
Internally ZF2 will combine/merge all module configuration into one complete array using array_replace_recursive(). Matching configuration keys will therefore be replaced by modules that have loaded after.
So you will also need to ensure that you have it correctly configured in application.config.php
array(
'modules' => array(
//...
'ZfcUser',
'ZfcUserOverride', // Loads after
// ...
),
);
Here the answer.
#Sharikov Vladislav, I want to say something to you.
In this question I answered to your question and you choose the correct answer to somebody that just update his answer with my content 10 hours later.
I do not want to start a flame war, what I ask is just to be correct to whom used its time to help you.
And also I think you must use search engines prior to post here, you are asking a question for every single step of your development process and it is clear you are putting no effort on searching a solution by yourself.
Just sayin..