14038

ZF2 routing configuration

Question:

I've just built a zf2 project and i'm having a configuration problem.

When i go to mydomain.com this routs as specified in configuration files to Application module, index controller, index action.

But If i type mydomain.com/otheraction this is not routed to Application module, index controller, otheraction action. Of course, since it isn't configured to do this.

So my question would be how do i configure the app to do that? I've added my Application/config/module.config.php file and the main config file. Thank you!

<strong>module.config.php</strong>

<?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ return array( 'router' => array( 'routes' => array( 'home' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Application\Controller\Index', 'action' => 'index', ), ), ), // The following is a route to simplify getting started creating // new controllers and actions without needing to create a new // module. Simply drop new controllers in, and you can access them // using the path /application/:controller/:action 'application' => array( 'type' => 'Literal', 'options' => array( 'route' => '/application', 'defaults' => array( '__NAMESPACE__' => 'Application\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( ), ), ), ), ), ), ), 'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), ), 'translator' => array( 'locale' => 'en_US', 'translation_file_patterns' => array( array( 'type' => 'gettext', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.mo', ), ), ), 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController' ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', ), ), );

<strong>application.config.php</strong>

<?php return array( 'modules' => array( 'Application', 'ZfcBase', 'ZfcUser', ), 'module_listener_options' => array( 'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ), 'module_paths' => array( './module', './vendor', ), ), );

Answer1:

The default route configured is to accept any url of this format, this is the standard format

mydomain.com/ mydomain.com/applicatiom/controller/action (standard format)

in your case mydomain.com/otheraction the router expect a module otheraction, but its not present.

if you need to have a route as specified above have a entry under route

'otheraction' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/otheraction', 'defaults' => array( 'controller' => 'Application\Controller\Index', 'action' => 'otheraction', ), ), ),

Answer2:

In the routes subarray, add a next entry similar to the 'home' entry, for example:

'user' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/user', 'defaults' => array( 'controller' => 'Application\Controller\User', 'action' => 'some-action', ), ), ),

That would route yourpage.com/user to someActionAction in your UserController.

Recommend

  • How to populate a jquery datatable using server side processing (PHP)?
  • Regex for instances in STEP-files?
  • Disabling mouse scrolling in Emacs
  • what is the equivalent of IP_TOS (PPROTO_IP option in setsockopt function for IPv4 socket) in IPv6 a
  • Can printf “%x\\n” \\'a be performed in awk?
  • Append to line that is preceded AND followed by empty line
  • Handling cron jobs in docker?
  • Adding a field to a structured numpy array (3)
  • How to show different validation messages for email validation in Angular2 using Validators class?
  • angular.js ui-route how catch route or url or params after reject?
  • Why does this code crash on the distributed app but work in the debugger?
  • Entity framework - Foreign key - data annotation
  • How to read files recursively in Java 7?
  • Pass large string into api controller by $http angular service
  • why route of backbone needs the default route
  • Manage different base layouts in Angular2
  • How to unit test a Angular 4 component which uses router.paramMap
  • Per Machine App Registration
  • Webpack-dev-server and isomorphic react-node application
  • React Router and Arbitrary Query Params: Page Refreshes Unintentionally on Load?
  • PWA with multiple pages
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • Can't locate Module/Build.pm in @INC (@INC contains: /usr/local/lib64/perl5
  • Installing PHP 7 on digitalocean
  • configure: error: no acceptable C compiler found in $PATH
  • Android full screen on only one activity?
  • Accessing IRQ description array within a module and displaying action names
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?