Havin in a Zend Module following structure
Name of Module
Test
config
src
view
layout
test
index
index.phtml
login
index.phtml
register
index.phtml
Module.php
Now when accessing website for example for login file it will be as following http://justsite.something.com/login
Problem Need to change to http://justsite.something.com/login/ for that i added to login folder another folder index and there putted index.phtml file like this
login
index
index.phtml
But that doesn't seem to work. Should i change in LoginController.php for this to work?
module.confing.php
'router' => array(
'routes' => array(
'test' => array(
'type' => 'Literal',
'options' => array(
'route' => '/test',
'defaults' => array(
'__NAMESPACE__' =>'Test\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'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(),
),
),
),
),
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/login/', // PUT HERE WHATEVER YOU WANT
'defaults' => array( // AND MAP THAT URL TO CONTROLLER/ACTION
'__NAMESPACE__' =>'Test\Controller',
'controller' => 'Login',
'action' => 'index',
),
),
),
),
),
Fixed Just added
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' =>
'/[:controller[/:action]/]',
Like #tasmaniski wrote you don't need to make changes to LoginController or whatever, all comes down to your route config. You are not the only one who wants "trailing slashes" in his routes, here you can read how you can achieve this.
Apparently you need to put it between []
'route' => '/[:controller[/[:action[/]]]]',
Hope that solves it for you.
NOTE Some people suggest using a rewrite rule for this, I thought this might be worth mentioning too.
No matter what is your forlder structure. You need to make a change in the config file module.config.php, section "routes".
You only need to put in "route" value with slash at the end and map it to controller/action.
'routes' => array(
'login' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/login/', // PUT HERE WHATEVER YOU WANT
'defaults' => array( // AND MAP THAT URL TO CONTROLLER/ACTION
'controller' => 'Application\Controller\Login',
'action' => 'index',
),
),
),
)
Related
What I'm trying to do is generate a URL using the url view helper in Zend Framework 2
The URL that I am trying to generate should be
my-website.com/app/## (where ## is equal to the "ownerID")
What happens when I generate this using the view helper like this:
$this->url('mymodule', array('action'=>'show', 'ownerID'=>'28'));
is it only generates "my-website.com/app", but I am expecting "my-website.com/app/28" based on the routing configuration.
Here is the route information from my module.config.php file
'router' => array(
'routes' => array(
'mymodule' => array(
'type' => 'segment',
'options' => array(
'route' => '/app',
'defaults' => array(
'controller' => 'MyModule\Controller\MyModule',
'action' => 'show',
),
),
// Defines that "/app" can be matched on its own without a child route being matched
'may_terminate' => true,
'child_routes' => array(
'archive' => array(
'type' => 'segment',
'options' => array(
'route' => '/:action[/:ownerID[/:clientID]]',
'defaults' => array(
),
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'ownerID' => '[0-9]+',
'clientID' => '[0-9]+',
)
),
),
'single' => array(
'type' => 'segment',
'options' => array(
'route' => '/:ownerID[/:clientID]',
'defaults' => array(
'action' => 'show',
),
'constraints' => array(
'ownerID' => '[0-9]+',
'clientID' => '[0-9]+',
)
),
),
)
),
)
),
The same behavior occurs when using $this->redirect()->toRoute.
All of the routes work as expected when you type them manually, it is just the generation of the URL that has me stumped.
To more directly answer the question, it doesn't do what you expect because you are only passing the top level route name ("mymodule") to the URL helper. The ownerID is part of a child route.
What you actually want is:
$this->url('mymodule/archive', array('action'=>'show', 'ownerID'=>'28'));
(or possibly mymodule/single, depending on the output you want).
I'm creating a new small application with Zend Framework 2 and I've some problem with routes...
I've created a a new module cloning the Skeleton Module with Git and renaming it "Users". I've added controllers to register a new user, to login and to perform CRUD operations.
This is my module folder structure:
and this is my module.config.php file:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Users\Controller\Index' => 'Users\Controller\IndexController',
'Users\Controller\Register' => 'Users\Controller\RegisterController',
'Users\Controller\Login' => 'Users\Controller\LoginController',
'Users\Controller\UserManager' => 'Users\Controller\UserManagerController',
),
),
'router' => array(
'routes' => array(
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
'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.
'login' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/login[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Login',
'action' => 'index',
),
),
),
'register' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/register[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\Register',
'action' => 'index',
),
),
),
'user-manager' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '/user-manager[/:action[/:id]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Users\Controller\UserManager',
'action' => 'index',
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'users' => __DIR__ . '/../view',
),
),
);
When I try with my browser the url test.local/users/login or test.local/users/register everything works fine but if I try test.local/users/user-manager I get the following error:
Route with name "user-manager" not found
whilst with test.local/users/user-manager/edit/5 it is rendered the correct page.
I'm bit confused and I don't know how to solve this problem.
Any help is very appreciated. Thanks in advance.
problem solved! #AlexP was right, the problem was in index.phtl file with $this->url('user-manager');, I've changed in $this->url('users/user-manager') and now it works.
Thanks again!
I think the issue is with constraints and default values. Try to remove constraints in user-manager route and see what happen.
I'm trying to achieve the routing explained in below but having a lot of trouble (the documentation is like a foreign language). Should routing for each module be kept separate in that module's config file?
"route": Module/Controller/Action
"/": Application/Index/index
"/:example": Application/Campaign/index (with param "campaign" = [example])
"/admin": Admin/Admin/index
"/admin/login": Admin/Access/login
"/admin/:controller/:action": Admin/[defined]/[defined]
I've tried to understand and use the skeleton application to do this and everything works up to the "/admin/controller/action" route where the script times out (I'm guessing there's a recursive loop in there somewhere). My route definitions are:
'home' => array(
'type' => 'literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'campaign' => array(
'type' => 'segment',
'options' => array(
'route' => '[/:campaign]',
'constraints' => array(
'campaign' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Campaign',
'action' => 'index',
'campaign' => ''
),
),
),
'admin' => array(
'type' => 'segment',
'options' => array(
'route' => '/admin',
'defaults' => array(
'controller' => 'Admin\Controller\Admin',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'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(
),
),
),
),
),
1) Yes, you should separate routing config to modules, ZF2 will merge it automatically. So you need to store home and campaign routes in Application/config/module.config.php and admin route in Admin/config/module.config.php
2) I don't see any global mistakes in your config. Two small things:
2.1) You can use literal instead of segment for admin parent route
2.2) If you want to use :controller as a route param, be sure to make shortcut for it via invokables section of config. For example:
'controllers' => array(
'invokables' => array(
'index' => 'Application\Controller\Index'
'admin' => 'Admin\Controller\Admin'
),
),
Now, for example, url /admin/admin/update is connected with Admin\Controller\Admin->updateAction()
3) I think it's not possible to make some recursive loops using only routing config. So check your code in controllers/services
I'm working on a Zend project and I have some issues with the router/routes.
I want the have these URLs:
www.mydomain.tld/contact/person
www.mydomain.tld/contact/person/formsend
www.mydomain.tld/contact/person/id
The third segment in the URL will be a fixed text "formsend" or it will contain an ID, which is a number like '123456789'.
URL 1 and 3 should both execute the indexAction(), URL 2 must execute the sendAction().
Now I have these route's setup to get URL 1 and 2 working:
return array(
'router' => array(
'routes' => array(
'contact' => array(
'type' => 'Segment',
'options' => array(
'route' => '/contact[/][:dns]',
'defaults' => array(
'__NAMESPACE__' => 'project\Controller',
'controller' => 'Contact',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'send' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/[:action[/]]]',
'defaults' => array(
'action' => 'send'
),
),
),
),
),
I have no idea how I must change my routes so I would be able to have segment_3 inside the indexAction(). What do I need to change?
It sounds to me you only need two routes:
First define /contact/:type/formsend, then /contact/:type[/:id].
You can make them children of a top-level (non-terminating) /contact or /contact/:type route, or not.
SO!
I installed zfcUser module. I tried to open site/user link. It worked. When I try to open site/user/ (added / symbol) it is not openning. Why?
I think, I have to edit router. I found this in module.config.php file in \vendor\zf-commons\zfc-user\config\ directory:
'route' => '/user',
How to change this, so it will allow both /user and /user/ urls?
Why it is possible to write both /application and /application/, but it is not specified in config file?:
'route' => '/application',
there are a couple of ways to make trailing slash works, one is creating a Segment type route marking the trailing slash as not mandatory, eg.
'users' => array(
'type' => 'Segment',
'options' => array(
// Change this to something specific to your module
'route' => '/users[/]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'index',
),
),
You can also add a child Literal route to another Literal route, like follow
'users' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/users',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'User',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
),
),
),
),
If you want the trailing slash on user work with ZfcUser you can extend its routing rules adding this configuration in any other registered module
'zfcuser' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
),
),
),
),