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

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");

Related

Is it possible to dynamically change view name or create a view that does not exist yet in phalcon?

I would like to know how can i do this in phalcon. I have a web site build with phalcon. All is working great now i stumbled upon a problem, here is what i need.
When a user clicks on a post that was created by another user. It takes him to this post with pictures and all things he entered to DB. I would like that in browser the name of this view is not like www.website.com/posts/index but that it is like www.website.com/posts/Nameofthepost, and like that for each other postings on the website. So that all posts (really ads) show their name up in browser. I hope i wrote everything understandable.
Appreciate all suggestions
That has to do with routing doesn't it? I modified this from my own code, I used grouping, you don't have to. I didn't test this code.
// routes.php
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule("__YOUR_MODULE__");
$router->removeExtraSlashes(true);
... your other routes ...
// posts group
$posts = new \Phalcon\Mvc\Router\Group(array(
'module' => '__YOUR_MODULE__',
'controller' => 'posts',
'action' => 'index'
));
// All the routes start with /post
$posts->setPrefix('/post');
$posts->add('/{postName}/:params', array(
'action' => 'index',
'params' => 2
));
// Maybe this will be enough for your needs,
// the one above has a catch all params, which
// has to be manually parsed
$posts->add('/{postName}', array(
'action' => 'index',
));
$posts->add('[/]*', array(
'action' => 'index',
));
$router->mount($posts);
unset($posts);
... other routes ...
return $router;
On your controller, you can get the postName param this way:
$this->dispatcher->getParam('permaPath');
As shown in the phalcon routing documentation, you can use regex in your routing config, something like this?
$posts->add('/{postName:[-0-6_A-Za-z]+}/:params', array(
'action' => 'index',
'params' => 2
));
So, only -_, 0-9, A-Z, a-z allowed for postName. If the URL had a comma in there or something, then route doesn't match, 404 page not found.

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

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

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