Zend Framework: Routing seems to interfere with rendering of Zend_Navigation menu - zend-framework

i have setup a route like below in bootstrap.php
$route = new Zend_Controller_Router_Route(
'users/:id',
array(
'controller' => 'users',
'action' => 'view'
)
);
$router->addRoute('viewUser', $route);
when i try to goto /users/1, and in view script do echo $page->getHref(), i get
Fatal error:
Zend_Controller_Router_Exception: id
is not specified in
D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\View\Helper\Navigation\HelperAbstract.php
on line 522
when i try to goto /users/view/id/1 it works ok. i am wondering why would rendering menu items from Zend_Navigation crash in something separate like routing?

ok i solved the problem with help from another post
basically, i need to add a default value for id
$route = new Zend_Controller_Router_Route(
'users/:id',
array(
'controller' => 'users',
'action' => 'view',
'id' => '0'
)
);
$router->addRoute('viewUser', $route);

Related

Zend Framework: Page not found

I have developed a web application with Zend Framework which root is http://www.demo31.com/validacion/demo31/ but when I call that url I've got the next error:
Page not found
Request Parameters:
array (
'controller' => 'validacion',
'action' => 'demo31',
'module' => 'default',
)
I want that the values of array would be next:
array (
'controller' => 'index',
'action' => 'index',
'module' => 'default',
)
And my .htaccess is correct.
So, what do I have to do what I want?
Zend framework normally operates as per routes. If a particular URL is not reaching your code, then you have to configure routes to do that.
$router = $front -> getRouter();
$routePage = new Zend_Controller_Router_Route('/:controller/:action', array(
/* ^ Things to notice
Only two parameters are
asked from the route */
'controller' => 'default',
'action' => 'index',
'module' => 'default' //Predefine the module as `default
));
$router -> addRoute('default', $routePage);
By default, ZF assumes that the app is located in the root of the domain so that's why it treats validacion as a controller.
Zend Framework in a subfolder

ZF Frontend and backend routing

I want something that sounds fairly simple to me but appears not to be.
My problem is that I need 2 routes voor my application:
Whenever the module is admin apply the following route:
$router->addRoute(
'backend',
new Zend_Controller_Router_Route('/:module/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => ':id', 'value' => ':value'))
);
Which works great. An example url could be: http://localhost/server/domains/demo/admin/images/album/3 where admin is the module, images the controller and so on.
All I want is that when a user goes to http://localhost/server/domains/demo he is redirected to the default module, index controller and index action. Everything after demo/ should be considered a single parameter (with unknown / possible).
I tried several things, from using Route_Regex, trying (.*) or (\d+), things I found all around online. Tried switching values, making them static, turning on/off removeDefaultRouter, but nothing worked. Below you can see my current bootstrap. Any ideas?
$router = $this->frontController->getRouter();
$router->removeDefaultRoutes();
$router->addRoute(
'backend',
new Zend_Controller_Router_Route('/admin/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => ':id', 'value' => ':value'))
);
$router->addRoute(
'frontend',
new Zend_Controller_Router_Route('/default/:controller/:action/(.*)', array('module' => 'default', 'controller' => 'index', 'action' => 'index'))
);
Backend works fine, but whenon the http://localhost/server/domains/demo/ I get the following error: No route matched the request
When given an answer, please explain why, because Zend_Route has always been a little vague for me. Thanks in advance!
Temp fix
Below the temporary fix that I use. It works exactly how I want, but I still believe that the same is achievable with Zend_Route without checking if the module is admin.
$router = $this->frontController->getRouter();
$uri = explode('demo/', $_SERVER['REQUEST_URI']);
$uri = (isset($uri[1])) ? explode('/', $uri[1]) : $uri[0];
if($uri[0] == 'admin')
{
$route = new Zend_Controller_Router_Route('/:module/:controller/:action/:id/:value', array('module' => 'admin', 'controller' => 'dashboard', 'action' => 'index', 'id' => null, 'value' => null));
$router->addRoute('router', $route);
}
else
{
$route = new Zend_Controller_Router_Route('/*', array('module' => 'default', 'controller' => 'index', 'action' => 'index'));
$router->addRoute('router', $route);
}
It is worth doing if you really have to use this routes. For normal usage you can just generate links by
Zend_Layout::getMvcInstance()->getView()->Url(array('module' => 'admin'));
Zend_Layout::getMvcInstance()->getView()->Url(array('module' => 'default'));
and so on.
If you are not ok with this, try routes chaining:
$router = Zend_Controller_Front::getInstance()->getRouter();
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
$request = Zend_Controller_Front::getInstance()->getRequest();
$frontRoute = new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request);
$backRoute = new Zend_Controller_Router_Route_Module(array('module' => 'admin'), $dispatcher, $request);
$route = new Zend_Controller_Router_Route('admin');
$router->addRoute('backend', $route->chain($backRoute));
$route = new Zend_Controller_Router_Route('default');
$router->addRoute('frontend', $route->chain($frontRoute));
Explenation:
Zend_Controller_Router_Route_Module is for definining modules routes for the application. By chaining it to normal route like new Zend_Controller_Router_Route('admin') you made links like /admin/[your_module_route] where [your_module_route] have defined defaults array('module' => 'admin') and can take other parameters too.
new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request); is defined as an default route on the standard router.

Multiple Routes with Standard Router (Zend Framework)

I am trying to set up multiple routes to the same controller in zend as such:
URL | Controller::Action
=================================================================================
http://mysite/tasks/:level/ | Objectives::Objectives
http://mysite/tasks/:level/:objective/ | Objectives::tasks
http://mysite/tasks/:level/:objective/:taskID/ | Objectives::view
I've tried the following:
<?php
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/:objective/:taskID/',
array(
'controller' => 'objectives',
'action' => 'view'
)
));
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/:objective/',
array(
'controller' => 'objectives',
'action' => 'tasks'
)
));
$router->addRoute('objectives', new Zend_Controller_Router_Route(
'task/:level/',
array(
'controller' => 'objectives',
'action' => 'tasks'
)
));
?>
However the last rule seems to overwrite the previous rules in the router.. I've read the Zend Documentation for the router over and over, I have a feeling im just missing something - should I be using a different router class?
Any help is Much Appreciated
As suspected I was overwriting the previous rules. The first argument for addRoute() is a name for the route, not the controller you are routing to as I thought. Giving each route a unique name fixed the problem.

Regex Routing - rule not being found

I'm defining regex routes for cleaning up my URLS. The idea is that all pages added by the user will be use the URL www.example.com/page-slug rather than using the actual controller, www.example.com/userpages/page-slug. Other pages will follow the standard module:controller:action routing scheme.
I'm trying to aceive this using router precedence.
I have defined the scheme below..
class Default_Bootstrap extends Zend_Application_Module_Bootstrap{
protected function _initRoute() {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter(); // returns a rewrite router by default
$route['index'] = new Zend_Controller_Router_Route_Regex(
'/',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
)
);
$route['contact'] = new Zend_Controller_Router_Route_Regex(
'contact/(\d+)',
array(
'module' => 'default',
'controller' => 'contact',
'action' => 'index'
)
);
$route['research'] = new Zend_Controller_Router_Route_Regex(
'research/(\d+)',
array(
'module' => 'default',
'controller' => 'research',
'action' => 'index'
)
);
$route['account'] = new Zend_Controller_Router_Route_Regex(
'account/(\d+)',
array(
'module' => 'default',
'controller' => 'account',
'action' => 'index'
)
);
$route['userpages'] = new Zend_Controller_Router_Route_Regex(
'/(.+)',
array(
'module' => 'default',
'controller' => 'userpages',
'action' => 'index'
),
array(
'slug' => 1
),
'%s'
);
$router->addRoute('userpages', $route['userpages']);
$router->addRoute('contact', $route['contact']);
$router->addRoute('research', $route['research']);
$router->addRoute('account', $route['account']);
$router->addRoute('index', $route['index']);
}
}
Things are generally working OK with the router precedence ensuring that index/account/research/contact pages are picking up the correct controller. However, when attempting to go to a URL covered by the "userpages" route e.g. "about-us", final catch all route is not being found resulting in...
Message: Invalid controller specified (about-us)
.
.
.
Request Parameters:
array (
'controller' => 'about-us',
'action' => 'index',
'module' => 'default',
)
Any idea where I'm going wrong here? It seems to me that the regex is correct "/(.+)" should be catching eveything that is not the index page.
EDIT: #phatfingers, OK you're right, I've edited "\d+" to ".+" to catch one or more of any character. The problem persists. In fact before changing the regex, I tried the URL www.example.com/52, and got the same error - "Invalid controller specified (52)". After the change - with code as per the edited snippet above, the rule is still failing to find any matches.
Drop the forward slash in the 'userpages' regex, i.e. just ('.+)
The quote is straight from the manual Zend Router and Router_Regex but afaik it also applies to all the routes.
Note: Leading and trailing slashes are trimmed from the URL in the
Router prior to a match. As a result, matching the URL
http://domain.com/foo/bar/, would involve a regex of foo/bar, and not
/foo/bar.

Pretty Zend Framework urls

I would like to have my urls like this:
/index
/contact
/articles
/articles/selection
...
Instead of:
/index/index
/index/contact
/articles/index
/articles/selection
...
Basically I have only one controller. Which solution is the best to perform this? (controllers and redirections, ZF routing, url rewriting, something else?)
Have a look at the documentation. The behaviour you want is configured as default in the default router:
http://framework.zend.com/manual/en/zend.controller.router.html
if the first param do not maps a module name, it will search for a controller and if this fails too, it is looking for an action in your IndexController.
Did you tried calling your url's like you want to?
What happens if you navigate to /index? Should be the same like /index/index
use zend routing :
$router = Zend_Controller_Front::getInstance()->getRouter();
$route_index = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('route_index', $route_index );
$route_articles = new Zend_Controller_Router_Route('articles/:action', array(
'module' => 'default',
'controller' => 'articles',
'action' => 'index'
));
$router->addRoute('route_articles ', $route_articles );