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

1 Answer 1

2

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', ) ) ) 
Sign up to request clarification or add additional context in comments.

8 Comments

This code will work for only index Action and will forbidden other Action
forbidden action? This has NOTHING to do with access control
The problem with Segment is when i put query string parameters like this:
@Sam is effectively right. You can create generic routes that examine the request path. These can be general enough so that you do not have to add a new route for each action. However, AFAIK, you cannot specify different routing on the query string. You'd have to create one action and then forward - not redirect - to different actions based upon the query string values. Also, AFAIK, this is no different than ZF1.
@fasi91 Modified the answer ;) Check again
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.