datatype in request parameter $id = $this->params()->fromRoute('id'); - zend-framework

so far I have worked only with integer values as request parameters in urls.
Now I need to work with strings which contain spaces etc. for example:
"xyz 666 888-VCT"
I send the route and parameters out of my view script:
PCB
I try to get it within my controller action:
$id = $this->params()->fromRoute('id');
How is the right way to catch a string value in this case?
edit:
here additional my routing paramters:
'pcb' => [
'type' => Segment::class,
'options' => [
'route' => '/pcb[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => Controller\PcbController::class,
'action' => 'index',
],
],
],
I get always
the requested url could not match by routing
My url looks like follows:
...pcb/index/3441 99901 B 03 2519 xyz
I'm sure the problem are the spaces, so my question a bit more detailed, can I quote or somewhat the routing parameter?

SOLVED:
my regular expression in the routing was wrong, I changed it to:
'id' => '[a-zA-Z0-9\-\. /]*',

Related

How to set a default action for SocialEngine module

I have created a module with two controllers: Index and Pay. There are actions for both controllers, i.e.
Index -> indexAction
Pay -> indexAction, callbackAction, etc.
I have defined routes in the module's manifest.php, although it seems defining the routes in that file makes no difference as those routes work correctly anyway. The problem is when I browse the root of the module i.e. http://example.com/pgateway, only a specific action from my second controller (PayController->callbackAction) is executed. Why is that and how can I make say IndexController->indexAction the default page when example.com/pgateway is browsed?
My route definitions (manifest.php):
'routes' => [
'pay_general' => [
'route' => 'pgateway/:controller/:action/*',
'defaults' => [
'module' => 'pgateway',
'controller' => 'pay',
'action' => 'index',
],
'reqs' => [
'controller' => '\D+',
'action' => '\D+',
],
],
'pay_callback' => [
'route' => 'pgateway/:controller/:action/*',
'defaults' => [
'module' => 'pgateway',
'controller' => 'pay',
'action' => 'callback',
],
'reqs' => [
'controller' => '\D+',
'action' => '\D+',
],
],
],
route should be unique, in each definition. :action means it would work with values as well as empty. Incase of empty it will use defaults.
In your case latest route is overriding.
Try removing pay_callback, it would work as in pay_general.
Convention is manage one route for a controller, and manage accordingly.

Add specific characters to zend router

Is here any way to use space or "!" in zend url?
There code i have for routing :
'route' => '/city[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
Now i can have this type of urls : /city/index/Paris , but if i put this - /city/index/New York, it doesnt work giving me a 404 error.
I have tried to put into 'id' this -
'[a-zA-Z][a-zA-Z0-9_-][ ]*'
or
'[a-zA-Z][a-zA-Z0-9_-][%20]*'
, but again 404.
EDIT
And if i add to constraints cyrillic characters, like this
'id' => '[a-zA-Zа-яА-я][a-zA-Zа-яА-Я0-9_-]*',
i cannot use url like /city/index/Москва.
Please help me.

Zf2 : Controller forwarding/calling generate 404 Error

I am implementing separate auth solution for two different modules described in another question by me.
Zend framework 2 : Add different authentication adapter for two different modules
Now in AuthListener file I write code for forward/call to an different controller/action if authentication failed. That is
$result = $this->adapter->authenticate();
if (!$result->isValid()) {
$response = $event->getResponse();
// Set some response content
$response->setStatusCode(401);
$routeMatch = $event->getRouteMatch();
$routeMatch->setParam('controller', 'First\Controller\Error');
$routeMatch->setParam('action', 'Auth');
}
Now I am getting 404 error - "The requested controller was unable to dispatch the request". First I think I do not added route for Error/Auth, but then I verified it got 404 for all other controller/action too. All are directly accessible through their respective route. But forwarding resulting in 404 error. One important thing - I sending authentication request through phpunit to make unit test cases.
UPDATE : route details :
'routes' => array(
'rest' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/rest[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'First\Controller\Index'
),
),
),
'error' => array(
'type' => 'segment',
'options' => array(
'route' => '/rest-error/[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'First\Controller\Error',
'action' => 'auth',
),
),
)
),
'controllers' => array(
'invokables' => array(
'First\Controller\Auth' => 'First\Controller\AuthController',
'First\Controller\Error' => 'First\Controller\ErrorController'
),
),
Module.php
$listener = $serviceManager->get('First\Service\AuthListener');
$listener->setAdapter($serviceManager->get('Rest\Service\BasicAuthAdapter'));
$eventManager->getSharedManager()->attach('First', 'dispatch', $listener, 100);
I also tried to use forward instead of above solutio, But that gives error for circular forward Circular forwarding detected: greater than 10 nested forwards. I think event called when forward called.
try to call your action like that :
$routeMatch->setParam('action', 'auth');
I think the problem might occure because you are listening to MvcEvent::EVENT_DISPATCH. In your listener you set new controller and action variables for RouteMatch but since you are already passed the route event changing those parameters will not have any effect.
You should listen to MvcEvent::EVENT_ROUTE instead and then it will probably work.

Zend routing (default controller to handle all url)

At first sorry for my poor English.
I want to handle all requests which do not match with my routes to CustomPageController.
I am trying to configure my module.config.php like this:
'defaults' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/*', // or 'route' => '/user*' for example, to handling www.site.com/user4 (just as example)
'defaults' => array(
'controller' => 'custom_page',
'action' => 'index',
),
),
),
To handle all queries that do not match with routes must redirect to my last "defaults" route. But it is not working.
You can use your ErrorController as an alternative for this. If the controller name is not found then you get the error code 500. You can then do whatever you want to do on that controller (re-route to your CustomPageController)

same controller name in different modules in zf2

I just disvovered today that zf2 doesn't realy like when 2 controllers have the same name even if they are not in the same module.
However, I need to be able to call
localhost/users/types
and
localhost/messages/types
For the moment, my two controllers have the same names.
I also discovered that whatever the name of the module is, I always get the result of messages/types, even with localhost/nonexistingmodule/types oO
Here is what my module.config.php looks like :
return array(
'controllers' => array(
'invokales' => array(
'messages' => 'Messages\Controller\MessagesController,
'messages' => 'Messages\Controller\TypesController,
),
),
'di' => array(
'instance' => array(
'alias' = array(),
),
),
'router' => array(
'routes => array(
'restful' => array(
'type' => 'Zend\Mvc\Router\Http\Segment'
'options' => array(
'route' => '/Messages/:controller[.:formatter][/:id],
'constraints' => array(
'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formater' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'module' => 'Messages',
),
),
),
),
),
'view_manager' => array( ... ),
);
I have tried to set 'module' => 'Messages' in constraints (we never know :p) but I had a 404 error.
The module.config.php of the Tasks module is the same but for tasks.
I originaly had aliases but I removed them after I read somewhere this was not very recomanded.
One more thing, this is a REST API, all my controllers extends AbstractRestfulController (in case this is important)
Anyone has an idea of how to make my 2 url to work?
Thanks :)
Aliases for invokables or services and anything else should be unique. If they are not unique they may be overwritten by another module in the order the modules themselfes are loaded. That means: when setting up invokables or any sorts of aliases, make sure that the aliases are unique and meaningful. Personally i name my Controllers like this:
'invokables' => array(
'mymodule-controller-controllername' => 'Mymodule\Controller\ControllernameController'
)
Same with Services or any other sorts of aliases
'services' => array(
'mymodule-service-servicename' => 'Mymodule\Service\Classname'
)
The Documentation follows a Namespace-styled syntax like...
'invokables' => array(
'Mymodule\Controller\Controllername' => 'Mymodule\Controller\ControllernameController'
)
...which personally i find totally confusing, as it resembles a Namespace way too much and doesn't really auto-imply that it is just an alias / key
Now your comments-question i do not understand. You want to match one route to two different controllers? That would be impossible and senseless.
Answer Update with routing config
As for routing configuration you have several possible ways. Personally i put lots of effort into building up literal routes, since they are the fastest, but also require a lot of manual attention. Alternatively there is segment routes which inhibit a little more magic to them. I will cover the literal approach for you:
Module Messages
'controllers' => array(
'invokables' => array(
'messages-controller-index' => 'Messages\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'messages' => array(
'type' => 'literal',
'options' => array(
'route' => '/messages',
'defaults' => array(
'controller' => 'messages-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
Module Tasks
'controllers' => array(
'invokables' => array(
'tasks-controller-index' => 'Tasks\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'tasks' => array(
'type' => 'literal',
'options' => array(
'route' => '/tasks',
'defaults' => array(
'controller' => 'tasks-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
So what's happening there is:
if route is /messages if routes to controller-alias messages-controller-index with indexAction()
if route is /messages/types it stays at controller-alias messages-controller-index but goes to typesAction()
if route is /tasks if routes to controller-alias tasks-controller-index with indexAction()
if route is /tasks/types it stays at controller-alias tasks-controller-index but goes to typesAction()
You could obviously change controller aliases and meanings behind that. If you'd want to add an ID for a route like /messages/types/1 you'd build a child-route to the types-route that is of type segment and check for an [:id] parameter whose constraints should be numeric :) Check the official ZF2 Manual for more information, i'm lazy now :P
tl;dr: Make sure you have a __NAMESPACE__ default in your route definition, and then make sure all your service names are prefixed with it. Usually, the value will match your module namespace.
For the longer explanation:
I'm going to be blunt: it's a really bad practice to have a dynamic segment in your route that maps to the controller (:controller in your example). The reason is that if somebody discovers this, they can do controller injection, and request a controller that should not be routed by this particular route.
Additionally, the practice can easily lead to collisions, particularly if you omit a __NAMESPACE__ default in your route configuration -- which, based on the question, you've already discovered.