How to make zend framework 2 advanced routing - php

i have a difficulty with zendframework 2 routing.
ex i need to make a dynamic route in zf2
/program1
/program2 etc etc.
controller the program name is a dynamic name. is there a way we can terminate all undefined routes to one controller/action ?

You can do something like this:
'myroute' => array(
'type' => 'regex',
'options' => array(
'regex' => '/(?<name>.*)',
'spec' => '/%name%',
'defaults' => array(
'controller' => 'My\Controller\Index',
'action' => 'index',
),
),
'priority' => -1
)
),

Related

How to pass parameters by url with Zend Framework 2

I'm getting into trouble with passing param by url in Zend Framework 2. For example : I want to access page by a URL like this:
http://example.com/student/details/aaa it's working, but if my url like this :
http://example.com/student/details/1 it's not working, And i still get a 404.
I tried to follow the instruction from how to pass params using a route in Zend Framework 2?
But it still doesn't work.
here's my module.config.php
'student' => array(
'type' => 'segment',
'options' => array(
'route' => '/student[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'AppFeeder\Controller',
'controller' => 'Student',
'action' => 'index',
),
),
),
And i use this $this->getEvent()->getRouteMatch()->getParam('id') in my controller.
Can anyone help me ?
You are using wrong route rule for id which says start from alphabet then alphabet+number+"_-".
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
Instead you need to change the rule that says start from alphabet+number then alphabet+number+"_-"
'id' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',

Zend Framework 2 - route parameters after <home> route

I am trying to solve the routing problem I am facing right now with no luck (googled for hours, saw dozens of examples and solved questions but none works for me - the closest one is this Routing Zend Framework 2 Language in url but even this is not working for me).
I have created an SSO application (I mean the authentication part) and now I am porting it to ZF2 app (I have it almost working as I workarounded the router but need now the final routing to be done) where only these types of URLs are possible:
http[s]://domain.com/login/asfgsdgdsfgzsdgdsf/
http[s]://domain.com/login/tdhjsdgbndfnfgdnhd/
http[s]://domain.com/logout/asfgsdgdsfgzsdgdsf/
http[s]://domain.com/info/dthnzdfbdfhgnfsd/
Those login, logout or info parts are not the controllers nor actions but the methods names I then call on SSO service from within my IndexController and indexAction(). The rest part of the URL is the client application hash.
Now here is my router config which only matches the home route and when the other parts (method name [and hash]) are provided, I got 404 from the ZF2 app (so at least I am in the app context).
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'SSO\Controller\Index',
'action' => 'index',
'act' => 'login', // by doing this I am able to workaround the router and work with SSO
'client' => '<my_secret_hash>', // by doing this I am able to workaround the router and work with SSO
),
),
'child_routes' => array(
'action' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '[/:act]',
'defaults' => array(
'act' => 'login',
),
'constraints' => array(
'act' => '(login|logout|info)?', // only login, logout or info are allowed
),
),
'child_routes' => array(
'client' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '[/:client]',
'constraints' => array(
'client' => '[a-zA-Z0-9]+',
),
'defaults' => array(
'client' => '<my_secret_hash>',
),
'may_terminate' => true,
),
),
),
),
),
),
),
);
(only router part of my module.config is provided...)
Now with that router only http[s]://domain.com[/] is matched and either of http[s]://domain.com/(login|logout|info)[/] or http[s]://domain.com/(login|logout|info)/adfbsDVdfbzdbxfbndzxbxfnb[/] matches into A 404 error occured.
Though I try to define the route parts as optional (just for the testing and development purposes), they should by required in the production environment. Anyway, I tried to define them NOT optional too, but didn't work either.
Question: How should I configure the router to match my routes (URLs) defined in the beginning?
Couple of things:
My guess is that you have too many slashes in there -- a child route of / doesn't need to be defined with a slash at the beginning since it's parent already got it
I don't see a reason for 2 nested routes (of course I don't know the rest of your config, so it's a guess)
home route probably should be Literal and able to terminate (again a guess)
there was one unmatched parenthesis (probably 'misspasted' or something)
I suppose this should work for you:
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Literal',
'may_terminate' => true,
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'SSO\Controller\Index',
'action' => 'index',
),
),
'child_routes' => array(
'whateva' => array(
'type' => 'Segment',
'may_terminate' => true,
'options' => array(
'route' => '[:act[/:client]]', // as Sam suggested
'defaults' => array(
'act' => 'login',
'client' => 'fsadfasf32454g43g43543',
),
'constraints' => array(
'act' => '(login|logout|info)',
'client' => '[a-zA-Z0-9]+',
),
),
),
),
),
),
),

Using route in Zend Framework 2

i need to route urls like in zf1.
in particular i need that these urls will be automatically redirect to appropriate actions without specify a new route every time.
/site/getData
/site/getData?param=5&par2=test
/site/getOther
...
So a segment route doesn't work, i've tried a Literal route but i can't reach a working solutions.
Anyone can help me?
Thanks a lot
This should be solved by a pretty default segment route like the one provided in the documentation.
'type' => 'Zend\Mvc\Router\Http\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(
'controller' => 'default-controller-alias',
'action' => 'index',
),
)
Now if you set up your controller names like the following:
'controllers' => array(
'invokables' => array(
'sites' => 'Namespace\Controller\SitesController',
'other' => 'Namespace\Controller\OtherController'
Then you should be able to achieve exactly what you want. And to create params to your route, you simply use the ViewHelper correctly ;)
$this->url('routename',
array(
'controller' => 'site',
'action' => 'getData'
),
array (
'query' => array(
'param1' => 'foo',
'param2' => 'bar',
'paramN' => 'baz',
)
)
)

Zend Framework 2 Routing subdomains to module

After searching a long time with no success.
before I give up, I would like to ask:
Is there a way to route a subdomain to a module in Zend Framework 2? like:
Subdomain => Module
api.site.com => api
dev.site.com => dev
admin.site.com => admin
site.com => public
...
I tried doing it like this but I can't get access to controllers other than the default (Index).
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'site.com',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
)
)
),
),
Thank you for taking the time to help me.
Zend Framework 2 doesn't have a notion of routing to modules; all routing mappings are between a URI pattern (for HTTP routes) and a specific controller class. That said, Zend\Mvc provides an event listener (Zend\Mvc\ModuleRouteListener) which allows you to define a URI pattern that maps to multiple controllers based on a given pattern, and so emulates "module routing". To define such a route, you would place this as your routing configuration:
'router' => array(
'routes' => array(
// This defines the hostname route which forms the base
// of each "child" route
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'site.com',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This Segment route captures the requested controller
// and action from the URI and, through ModuleRouteListener,
// selects the correct controller class to use
'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(
'controller' => 'Index',
'action' => 'index',
),
),
),
),
),
),
),
(Click here to see an example of this # ZendSkeletonApplication)
This is only half of the equation, though. You must also register every controller class in your module using a specific naming format. This is also done through the same configuration file:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
The array key is the alias ModuleRouteListener will use to find the right controller, and it must be in the following format:
<Namespace>\<Controller>\<Action>
The value assigned to this array key is the fully-qualified name of the controller class.
(Click here to see an example of this # ZendSkeletonApplication)
NOTE: IF you aren't using ZendSkeletonApplication, or have removed it's default Application module, you will need to register the ModuleRouteListener in one of your own modules. Click here to see an example of how ZendSkeletonApplication registers this listener
If i understand slide #39 of DASPRIDS Rounter Presentation correctly, it's as simple as - on a per module basis - to define your subdomain hosts, i.e.:
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'api.site.com',
'defaults' => array(
'__NAMESPACE__' => 'Api\Controller',
'controller' => 'Index',
'action' => 'index',
),
)
)
),
),
Etc, you'd do this for every Module on its own.

Zend Framework 2 Part Route Assembly

My Zend Framework 2 application has a route definition that's trying to mimic the default Zend Framework 1 route. It looks like:
'router' => array(
'routes' => array(
'default' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'wildcard' => array(
'type' => 'wildcard',
),
),
),
),
),
It matches routes just fine, but I can't assemble routes with arbitrary parameters using the Url view helper.
For example,
$this->url('default', array('controller' => 'test', 'action' => 'test', 'id' => 5));
results in /test/test instead of /test/test/id/5.
Does anyone know how to assemble partial routes like this? Or is there a better way of getting ZF1-style routes?
It turns out that you need to specify the entire route name (including child routes) in the Url view helper.
Using the router defined in my question, the proper view helper call would look like:
$this->url('default/wildcard', array('controller' => 'test', 'action' => 'test', 'id' => 5));
which would result in a url of /test/test/id/5.

Categories