Zend Framework - Param not specified - php

I got a little problem here. I'm currently trying to use the Zend Router Rewrite but I got this error:
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'alias is not specified'
Here's my code for the route:
// BLOG -> CATEGORIES -> LIST ARTICLES
$route = new Zend_Controller_Router_Route(
'/blog/categories/:alias',
array(
'module' => 'blog',
'controller' => 'categories',
'action' => 'list'
)
);
$router->addRoute('blog-categories-list', $route);
The URL i'm trying to access is: /blog/categories/general/.
Why am I getting this error?
P.S.: I didn't specify a default value for :alias 'cause I have this route also:
// BLOG -> CATEGORIES
$route = new Zend_Controller_Router_Route(
'blog/categories/',
array(
'module' => 'blog',
'controller' => 'categories',
'action' => 'index'
)
);
$router->addRoute('blog-categories', $route);

This error is probably coming from somewhere where you are trying to output a link using the route, such as the URL helper:
$this->url(array(), 'blog-categories-list')
It's telling you that you need to pass 'alias' to this as well.

Related

Zend Framework - how to tell nice looking url to go to static url?

I have this following routing. How to tell /nice-looking-url to load /home/www/html/project/public/static/index.html which is not in the Controller
$route = Http\Literal::factory(array(
'route' => '/nice-looking-url',
'defaults' => array(
'controller' => 'Application\Controller\Nicelookingurl',
'action' => 'index'
),
));
$router->addRoute('nicelookingurl', $route, null);
The simple way to do so is to add this line
return $this->renderScript('insert-your-script-path-here');
as first line in your Nicelookingurl index action
If you want to disable the layout you can also add
$this->_helper->layout()->disableLayout();

Unable to get parameters using Zend Routing Chain

In my bootstrap file, I have the following routing chain. The desired behaviour is to send any request through /usa/:controller/:action to the local module.
For instance, when http://{hostname}/usa/index/index is called, the request goes through the local module, index controller, index action.
The problem I am having is adding parameters. For instance, when I request http://{hostname}/usa/index/index/id/5 to try to get the id parameter, I get the following error message:
An Error occurred. Page not found. Exception information: Message: Invalid controller specified (usa) with the following request params:
array (
'controller' => 'usa',
'action' => 'index',
'id' => '5',
'module' => 'default',
)
How can I set up the chain routing in order to still utilize other parameters?
Here is my code in the application bootstrap:
protected function _initRouting(){
$router = Zend_Controller_Front::getInstance()->getRouter(); // Get the main router from the front controller.
$router->addDefaultRoutes(); // Don't forget default routes!
//get default local route (directs to the local module)
$defaultLocalRoute = new Zend_Controller_Router_Route(
'/:controller/:action',
array(
'module' => 'local',
'controller' => 'index',
'action' => 'index'
)
);
$regionRoute = new Zend_Controller_Router_Route(
'/usa/',
array('region' => 'usa')
);
//chain this region route to the default local route that directs to the local module
$fullRegionRoute = $regionRoute->chain($defaultLocalRoute);
//add the full route to the router (ie. hamiltonRoute, atlanticcaRoute)
$regionRouteName = 'usaRoute';
$router->addRoute($regionRouteName, $fullRegionRoute);
}
Adding a * to the end of the $defaultLocalRoute was able to fix this issue for me.
//get default local route (directs to the local module)
$defaultLocalRoute = new Zend_Controller_Router_Route(
'/:controller/:action/*',
array(
'module' => 'local',
'controller' => 'index',
'action' => 'index'
)
);
Now when going to http://{hostname}/usa/product/view/id/5, the request goes to the desired location ->
module: 'local',
controller: 'product',
action: 'view',
params: array('id'=>5)

Zend Router - URL with or without parameters are two differents routes

I'm currently creating a new version of my website using Zend Framework and I'm stuck with a little problem I've seen in the past.
There are my routes: (a part)
// BLOG -> CATEGORIES
$route = new Zend_Controller_Router_Route(
'blog/categories',
array(
'module' => 'blog',
'controller' => 'categories',
'action' => 'index'
)
);
$router->addRoute('blog-categories', $route);
// BLOG -> CATEGORIES -> LIST ARTICLES (:alias = name of the category)
$route = new Zend_Controller_Router_Route(
'blog/categories/:alias',
array(
'module' => 'blog',
'controller' => 'categories',
'action' => 'list',
'alias' => null
)
);
$router->addRoute('blog-categories-list', $route);
The problem is that: when I go to /blog/categories/, it brings me the list action. What I don't want. I need the index.
Is there a way to fix that without using, for exemple, /blog/categories/view/:alias ?
Note: I have the same problem for /blog/ (list all articles) and /blog/:alias/ (display single article).
By including 'alias' => null you're specifying a default value for the :alias parameter, used if it is not in the URL. This is why your second route is always matching. Remove this and it should work as you are wanting it to.

Zend Url View Helper not working with Regex Routes

I'm using Zend Framework 1.12 and have this route:
$router->addRoute('item_start',
new Zend_Controller_Router_Route_Regex(
'(foo|bar|baz)',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
1 => 'area'
),
'%s'
)
);
Problem is, when I call '/foo' and use the Url Helper in the View, it doesn't give me any parameters:
$this->url(array("page"=>1));
// returns '/foo' (expected '/foo/page/1')
$this->url(array("page"=>1), "item_start", true);
// also returns '/foo'
Any idea how to get the page-parameter into the URL? I can't use the wildcard like in the standard route, can't I?
In addition to David's suggestions, you could change this route to use the standard route class, and then keep the wildcard option:
$router->addRoute('item_start',
new Zend_Controller_Router_Route(
':area/*',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
'area' => '(foo|bar|baz)'
)
)
);
// in your view:
echo $this->url(array('area' => 'foo', 'page' => 1), 'item_start');
Your Regex route doesn't have a page parameter, so when the url view-helper ends up calling Route::assemble() with the parameters you feed it, it ignores your page value.
The two choices that come to mind are:
Modify your regex to include a (probably optional with default value) page parameter
Manage the page parameter outside of your route in the query string.

Zend framework router problem - strange routing behavior

I've set the following routes for in Zf:
$router->addRoute(
'page',
new Zend_Controller_Router_Route('stranka/:niceuri/:id', array('controller' => 'page', 'action' => 'index'))
);
$router->addRoute(
'cat',
new Zend_Controller_Router_Route('kategoria/:niceuri/:id', array('controller' => 'category', 'action' => 'index'))
);
The problem is that the 'cat' route keeps overwriting the other 'page' route and simle $this->url() routes aswell. That means, that any links using the 'page' route and having the param 'niceuri' defined have the the value of 'niceuri' equal to the currently open page using the 'cat' route - which they sholdn't have. (sorry, does that make sense to you?) Any ideas on how to solve this behavior? Thanks a lot.
I didn't exactly understand what did you mean, but...
When you calling $this->uri helper in view you can set the name of the preffered router to use to assemble the url. Something like this:
echo $this->uri(array('niceuri' => 'Ololo', 'id' => '123'), 'page');
Hope this helps.

Categories