Zend framework router 404 error - php

Here is my zend framework module config:
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'default' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
]
),
)
...
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
and my IndexController.php looks like this:
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
public function testAction()
{
echo 1;
return 1;
}
}
I want get a right response when I access http://host/Index/test, but now I get a 404 error and a description like:
"The requested controller could not be mapped to an existing controller class ...".
What's the problem?

Try to change 'type' => 'segment' to 'type' => 'Segment'

Related

zend framework 3 segment routing doesn't work

I have the following module.config.php :
return [
'router' => [
'routes' => [
'landingpage' => [
'type' => Segment::class,
'options' => [
'route' => '/landingpage[/:action/:id]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => Controller\LandingPageController::class,
'action' => 'index'
]
],
'may_terminate' => true,
]
]
],
'controllers' => [
'factories' => [
Controller\LandingPageController::class => LandingPageControllerFactory::class
]
],
'service_manager' => [
'invokables' => [
'LandingPage\Service\LandingPageService' => 'LandingPage\Service\LandingPageService'
]
]
];
I am trying to use the following route and it doesn't work:
http://localhost:8081/landingpage/show/1CGe2cveQ
If I use the following route it works :
http://localhost:8081/landingpage/show
If I use the previous route with a / it doesn't work:
http://localhost:8081/landingpage/show/
If you need more info let me know.
Thanks.
You have a double slash in the route declaration: the route is matched by /landingpage/ followed by /:action/:id. If you remove this double slash, the route will work as expected.
'route' => '/landingpage[/:action/:id]',
Moreover, I'd suggest you to modify the route declaration to make the id optional:
'route' => '/landingpage[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]+'
]
Tested:
config
'landingpage' => [
'type' => Segment::class,
'options' => [
'route' => '/landingpage[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index'
]
],
'may_terminate' => true,
],
IndexController:
public function indexAction () {
print '<pre>' . print_r($this->params()->fromRoute(), true);
die();
}
public function showAction(){
print '<pre>' . print_r($this->params()->fromRoute(), true);
die();
}
Calling /landingpage
Array
(
[controller] => Application\Controller\IndexController
[action] => index
)
Calling /landingpage/show
Array
(
[controller] => Application\Controller\IndexController
[action] => show
)
Calling /landingpage/show/1CGe2cveQ
Array
(
[controller] => Application\Controller\IndexController
[action] => show
[id] => 1CGe2cveQ
)
Don't forget to clear the configuration cache, if it enabled ;)

ZF3 How to use optional parameter in homepage route

I'm having trouble adding an optional parameter to my home route.
This is my current router:
'routes' => [
'home' => [
'type' => Segment::class,
'options' => [
'route' => '/[:salon/]',
'constraints' => [
'salon' => '[a-zA-Z][a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => 'Application\Controller\Index',
'action' => 'index',
'salon' => 'test'
],
],
],
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:controller[/:action]]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
],
'defaults' => [
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Application\Controller\Index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'default' => [
'type' => 'wildcard'
]
]
],
],
My Controller:
<?php
namespace Application\Controller;
class IndexController extends AbstractController
{
public function indexAction()
{
var_dump($this->params('salon'));
die;
}
}
domain.ltd/
This works and I'm getting default value for salon paramter which is 'test'
domain.ltd/test123
Expected value would be 'test123' but this displays me 404 error: The requested URL could not be matched by routing.

Add new controller to existed module in zf3

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.

ZF2 Have all routes resolve to one controller and action

I'm trying to develop and single page app with AngularJS and ZF2. I'm having trouble with the routing. My idea is to have routes like /:controller/:action resolve to a single action. I've read the docs and tried the different routing types.
My idea is to have all /:controller/:action to resolve to App\IndexController\IndexAction so only the Angular init code is returned. After the initial page has loaded, Angular will send a secondary request to /template/:controller/:action to retrieve the template and /api/:controller/:action to gather to template's data.
Below is an example of the Regex route I attempted to make work.
'app' => array(
'type' => 'Regex',
'options' => array(
'regex' => '/(?[a-zA-Z][a-zA-Z0-9_-]*\/[a-zA-Z][a-zA-Z0-9_-]*)',
'defaults' => array(
'controller' => 'App\Controller\IndexController',
'action' => 'index',
),
'spec' => '/'
),
'may_terminate' => true,
),
Just use a Segment route:
'app' => array(
'type' => 'Segment',
'options' => array(
'route' => ':controller-fake/:action-fake',
'defaults' => array(
'controller' => 'App\Controller\Index',
'action' => 'index',
),
),
),
Why you just don't define 3 major routes of segment type. See the code below:
'all' => [
'type' => 'Segment',
'options' => [
'route' => '/app/[:controller[/:action]]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => 'App\Controller\IndexController',
'action' => 'index'
],
],
],
'template' => [
'type' => 'Segment',
'options' => [
'route' => '/template/[:controller[/:action]]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [],
],
],
'api' => [
'type' => 'Segment',
'options' => [
'route' => '/api/[:controller[/:action]]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [],
],
]

routing in zendframework 2 is not working

/* Here is my module config */
'controllers' => array(
'invokables' => array(
'User\Controller\User' => 'User\Controller\UserController',
),
),
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Literal',
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'User\Controller\User',
'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(
),
),
),
),
),
),
),
And Controller as
class UserController extends AbstractActionController{
public function indexAction(){
parent::indexAction();
return new ViewModel();
}
public function addAction(){
return new ViewModel();
}
}
whenever I try to access zf.localhost/user/user/add
It throws error as
Page not found.
The requested controller could not be mapped to an existing controller class.
Controller:
user(resolves to invalid controller class or alias: user)
No Exception available
I can't figure out why the routing is not working.
You are trying to access controller named user which doesn't exists. You have two options:
Change 'route' => '/[:controller][/:action]' to 'route' =>
'/:action'. And it will search for an action in your User\Controller\UserAction
Add to controllers new alias 'aliases' => array('user' => 'User\Controller\User')

Categories