zend framework url helper - how to pass variable before controller? - php

I have specific problem with Zend Framework (1.12) - url helper.
I have site, divided by languages in this form:
/en/contact
/de/gallery/gal1
etc..
I have changed router to accept first parameter as variable and it's accessible by GET.
How can I set URL helper to follow this guideline and pass this variable before controller in URL ? (I'm not using modules).
Many thanks.
Ivan

Let me reply to myself. Router setup:
$router = $frontController->getRouter();
$routeLang = new Zend_Controller_Router_Route(
':lang/:controller/:action/*',
array(
'lang' => 'de',
'controller' => 'index',
'action' => 'index'
),
array('lang' => '[a-z]{2}')
);
$router->addRoute('lang', $routeLang);
URL creation:
$this->url(array('lang' => 'bar', 'controller'=>'contact','action'=>'index'));
Please correct me, if my understanding is wrong.
Ivan

Related

PHP ZF2 - How to create dynamic urls

I have been tasked to explore the possibilities of creating dynamic urls.
What we try to achieve is serving a domain for the client.
eg. www.example.com/clients/john/
eg. www.example.com/clients/bill/
These urls are not fixed, but have to be made dynamically everytime a Client registers at our site. Our URL's are build of Controllers and Actions. Like www.example.com/controller/action.
How can this be achieved in the PHP Zend Framework 2?
Big thanks in advance!
All you need is the ZF2 Manual:
I suggest to use segment routes
You can define them like:
'client' => array(
'route' => '/clients/:username',
'constraints' => array(
'username' => '[a-zA-Z][a-zA-Z0-9-]+', //accepted value pattern
),
'defaults' => array( //which action to invoke
'controller' => 'Application\Controller\ClientController',
'action' => 'showclient',
),
));
Then you can create an url with url helper like:
$this->url('client', array('username' => 'John'));
And in your controller you will be able to get username param with:
$this->params()->fromRoute('username');

Avoid using "/index/index" twice in Zend Framework Routes

I am new to Zend Framework. I have a page that is:
http://localhost/demo/public/index/index/catid/art
I want to change that into
http://localhost/demo/public/art
I have no idea how to do it.
Also, why does it put index twice? Even my pagination has it, like:
http://localhost/demo/public/index/index/page/2
In my opinion is a little bit annoying. I would like the pagination to be
http://localhost/demo/public/page/2
Is there a way to do that? Thanks!
The default route works by using:
/module/controller/action
So if you have a module called "default", and your controller called "index", and an action called "index", then the most verbose way to refer to that specific action would be:
/module/controller/action
In order to set up a route you would use:
$route = new Zend_Controller_Router_Route(
'page/:page',
array(
'module' => 'default'
'controller' => 'index',
'action' => 'index'
),
array(
'page' => '\d+'
)
);
You could then get the page param in your controller using
$this->getRequest->getParam("page");

Routing in Zend Framework 2, skip 'index' action in url but get id

I have a controller that can be invoked as modulename/xmlcoverage with index action and some other actions, let say testAction().
The url to this controller is xml/coverage.
The default way is then that xml/coverage maps to my index action. And that xml/coverage/test maps to testAction. If I need an id for testAction, the url would be like xml/coverage/test/33 for instance.
However, for index action, it would need to be xml/coverage/index/33
Where I would like it to be xml/coverage/33.
This is my route
'xmlcoverage' => array(
'type' => 'segment',
'options' => array(
'route' => '/xml/coverage[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'modulename/xmlcoverage',
'action' => 'index',
),
),
),
When trying the url xml/coverage/33, I believe 33 should map to id, as it doesn't match the regex of action and both are optional. And since it doesn't match an action, the default (index) should be used.
Instead I get an error saying that the url cannot be matched by routing.
So for me, it acts as if the route was '/xml/coverage[/:action[/:id]]' because I for some reason have to specify action for it to recognize the id.
What am I doing wrong, and how can I get the urls to work as I'd like?
Thanks.
EDIT: Here is a problem.
Doing this in my view:
$this->url('xmlcoverage', array('action' => 'index', 'id' => $someid))
actually gives an URL on the form xml/coverage/1 which will crash!
Changing the route to /xml/coverage[/:action[/:id]] will at least make the url helper produce working urls..
After talking and debugging with the nice ZF2 folks on IRC, we tracked down a bug in the routing.
During the discussion I made a small example to my issue, which is here.
As you can see from the var dump here, the action gets lost in the second case where it should default to "index".
But if anyone needs this functionality to work right now, here are ways that fix it:
Instead of having the route to be /test[/:action][/:id] have it to be /test[/:action[/:id]], then the url helper will add /index/ and at least it works.
Make a new route where you only listen for /test[/:id] in addition to the other one.
In your controller, do public function notFoundAction() { $view = new ViewModel($this->indexAction()); //etc} Kinda hacky, but with this bug it will dispatch a not found action, which you can piggyback.

Zend - outputting better module url?

I am currently outputting the url of a particular module like so:
$this->view->url(array('controller'=>'index','action'=>'index','module'=>'somemodule'))
The problem is when I view:
/somemodule/action1/paramid/paramval
The url is showing as:
/somemodule/index/index/paramid/paramval
When all I really want is:
/somemodule/
Any ideas? I have used the above url array because I am trying to force it to display the correct url. sadly, simply array('module'=>'somemodule') doesn't work...
Weird, I would've thought it defaults to index/index if they aren't specified. You could try setting up something with Zend_Controller_Router_Route;
$router = $ctrl->getRouter(); // returns a rewrite router by default
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
You will however need to set up routes to capture the :id/:val into variables. You can do this with another addRoute(), naming each variable prepending a colon.
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule/:id/:val',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
Then you should be able to view the module without the index/index in the URL which might fix the problem with the url helper.
More info here http://framework.zend.com/manual/en/zend.controller.router.html

Zend Framework - Optional Router Labels

Very likely I'm going about this in the wrong way entirely. I'm completely new to the framework..
The site I am developing has two "parts" that are mainly separate. An informational/community half, and a commerce half. I'm using the following directory structure:
--application
----default
------controllers
------layouts
------models
------views
----store
------controllers
------layouts
------models
------views
--config
--library
--public
I would like to have a URL structure when browsing for products as follows:
/view/category/model/revision
This would pull up a specific product/revision - but I would like to back-track as well (browsing all revisions, all models, etc). I can't figure out how to achieve this.. My route is setup like this:
Bootstrap.php
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route = new Zend_Controller_Router_Route(
'view/:cid/:sku/:rev',
array('module' => 'store', 'controller' => 'index', 'action' => 'index')
);
$router->addRoute('view', $route);
This works fine for pulling up a specific product, but throws an exception (it reverts to the default module and complains that the controller 'view' does not exist) when leaving out any of the 3 labeled parameters. Is it possible to put in optional labels, where it would continue to use the view controller under the store module for 1-3 parameters? Am I missing the point?
I found nothing in the framework docs, but I wouldn't be surprised if I just couldn't find the page.. There's something about the Zend Framework documentation that drives me crazy.
Thank You
I'm not really a ZendFramework guy, but it's obvious the missing parameters are causing the issue. Routes are matched in reverse order. Could it be passing a NULL value to the view when 3 parameters are passed and it is expecting 4?
What if you tried something like:
$route = new Zend_Controller_Router_Route(
'view/:cid/:sku/:rev',
array('module' => 'store', 'controller' => 'index', 'action' => 'index', 'cid' => 0, 'sku' => 0, 'rev' => 0)
);
It should pass default values if they are not provided.

Categories