Followed the Cake Book example almost exactly.
Router::mapResources('incidentReports');
Router::parseExtensions('json');
Both before
require CAKE . 'Config' . DS . 'routes.php';
My controller called IncidentReportsController
class IncidentReportsController extends AppController {
Which contains functions
index()
view($id)
add()
edit($id)
delete($id)
Going to the URL
www.myurl.com/incidentReports.json
Sends the request to the index() function as expected.
Going to the URL
www.myurl.com/incidentReports/260.json
Should map to the view() function but trys to map to a 260() function which doesn't exist.
www.myurl.com/incidentReports/view/260.json
Does map to the view() function and works properly. However, my understanding is the "view" in the URL shouldn't be necessary.
Had the same issue. In my case I was able to fix it by changing the controller name in the URL.
Didn't work: http://www.example.com/entityName.json
Works fine: http://www.example.com/entity_name.json
According to the documentation, you've done everything right, so I'm not sure. Try putting this (the routes that should be enabled) in your routes too.
Router::resourceMap(array(
array('action' => 'index', 'method' => 'GET', 'id' => false),
array('action' => 'view', 'method' => 'GET', 'id' => true),
array('action' => 'add', 'method' => 'POST', 'id' => false),
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('action' => 'update', 'method' => 'POST', 'id' => true)
));
If your controller is within a plugin, you must specify as well..
e.g:
Router::mapResources('Plugin.Controller');
Related
I am using ZendFramework 2.x and trying to add a route to some existing ones. Moreover I want to put some parameters in die URL as well. If I use the segment-type for my new route ('showroom') I am able to call my new URL and will get forwarded to the corresponding view. Unfortunately I am not able to set some parameters in the URL. The other option is to use segment type in my module.config.php-file, but I will get some ZF-2-Exception, that my route is not configured correctly, this happens even before rendering my intro-view. Thanks in advance for showing me, how to combine segment and literal-type usage in route-child-route-combination or for telling me how to add parameters to literal type URLs.
Route-configuration in module.config.php of Module:
'router' => array(
'routes' => array(
'zfcuser' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'showroom' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:id',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'showRoom',
'id' => '1',
),
),
),
'login' => array(
'type' => 'Literal',
UPDATE: snippet which triggers the error (see comments)
<?php
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
$roomURL = 'zfcuser/showroom' . '/' . $roomIndex;
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url($roomURL) . "'>Betreten »</a></p>";
$roomIndex++;
}
?>
I'm not sure if this is the cause of your error, but I don't think you're using the URL helper correctly. You don't build a URL and pass it to the helper, the idea is the helper builds the URL for you using the route name and params. You probably want something like this:
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url('showroom', array('id' => $roomIndex)) . "'>Betreten »</a></p>";
$roomIndex++;
}
I have a problem with custom zend router.
this is my cat router
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute('categories', new Zend_Controller_Router_Route(
'video/k/:id/:title',array(
'controller' => 'video',
'module' => 'default' ,
'action' => 'k',
'id' => '',
'title' =>''
)
));
$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
when i try htttp://dev.dummy.com/video/k/1/foo works fine but
$this->url(array_merge($params, array('order' => 'title'))) or
$this->url(array_merge($params, array('order' => 'title')),'categories')
$this->url(array_merge($params, array('order' => 'title')),'categories', true)
doesnt return htttp://dev.dummy.com/video/k/1/foo/order/title
still returning htttp://dev.dummy.com/video/k/1/foo.
Hope this help.
Thanks.
Try setting the third parameter $reset of the url-helper to true:
$this->url(array_merge($params, array('order' => 'title')), 'categories', true)
This will reset the default parameters.
(Documentation at http://framework.zend.com/manual/1.12/en/zend.view.helpers.html#zend.view.helpers.initial)
When doing the array_merge, the integer keys will be reset to start from beginning. Just append your array instead of the array_merge:
$router->addRoute('categories', new Zend_Controller_Router_Route(
'index/test/:id/:title',array(
'controller' => 'index',
'module' => 'default' ,
'action' => 'test',
'id' => '',
'title' => ''
)
));
echo $this->url(array('order' => 'title'));
Tried it with http://dev.dummy.com/video/k/1/foo and http://dev.dummy.chom/video/k/1/foo/order/title/. In both cases, the url helper output was - htttp://dev.dummy.com/video/k/1/foo/order/title
In my Bootstrap I have
$route = new Zend_Controller_Router_Route(
':language/:country/:controller/:action/*',
array(
'language' => 'en',
'country' => 'us',
'controller' => 'bicycle',
'action' => 'index'
),
array(
'language' => '[a-z][a-z]',
'country' => '[a-z][a-z]'
)
);
Somewhere in my view I have
echo $this->url(array('page'=>2));
//actually this translated to $route->assemble(array('page' => 2), null, false);
The problem, is when I have some GET parameters: they won't be considered in the building of the link, and this is what I actually want.
Example:
I access the URL (in the browser)
http://localhost/myproject/en/us/controller/action/?get1=gval1&get2=gval2&get3=gval3
and the assembled URL is
http://localhost/myproject/en/us/controller/action/page/2
INSTEAD of
http://localhost/myproject/en/us/controller/action/page/2/get1/gval1/get2/gval2/get3/gval3/
or (I would prefer the next one)
http://localhost/myproject/en/us/controller/action/page/2/?get1=gval1&get2=gval2&get3=gval3
Any ideas?
Of course one solution (with Apache) would be to call this in my view:
$this->url(array(page=>2)) . ($_SERVER['QUERY_STRING']?$_SERVER['QUERY_STRING']:"")
but you cannot be sure this will always be included in the $_SERVER variable.
I want to make customer route for url which have parameters.
<a href='javascript:void(0)' onclick="window.location='<?php echo
$this->url(array('module' => 'courses', 'controller' => 'course', 'action' => 'add',
'std_id' => $entry['std_id']), 'coursesadd', TRUE); ?>'">add course</a>
and here what I make routing
$route = new Zend_Controller_Router_Route('courses/std_id/:std_id',
array('module' => 'courses', 'controller' => 'course', 'action' => 'add'));
$routesArray = array('coursesadd' => $route );
$router->addRoutes($routesArray);
But It doesn't route correctly!
Your code looks fine, except what RockyFord sad - You should only pass these parameters to url() helper which are required by designated route (when 'default' route is used indeed it requires module, controller, action, but Your 'coursesadd' route requires only std_id parameter). Moreover You should improve Your route a little bit to:
$route = new Zend_Controller_Router_Route('courses/:std_id', array(
'module' => 'main',
'controller' => 'product',
'action' => 'add',
'std_id' => null //default value if param is not passed
));
The param_name/:param_value construction in Your route is redundant because You already named the parameter, so by using above route You'll get std_id param in controller using
$this->_getParam('std_id')
Default value in route definition would save from throwing Zend_Controller_Router_Exception: std_id is not specified but it looks like $entry['std_id'] is not set.
I have got a problem with the zend navigation.
I use the zend navigation, and its ok when i call something like this in the url:
www.website/article_new, www.website/article_list,
www.website/friends_new, ...
But I want to call the site with some params like this:
www.website/article_new/123452/2335/45633246,
www.website/friends_new/23453/3453524/34554
I try it about to set some params, but this doesnt function.
I put the navigation in the bootstrap and in the layout I use it. Its a Navigation with some breadcumps where display:true and display:false.
Here is a code from my bootstrap:
$navigation = new Zend_Navigation(array(
array(
'label' => 'Home',
'controller' => 'index',
'action' => 'index',
'class' => 'menuF'
),
array(
'label' => 'Article',
'controller' => 'Article_List',
'id' => 'article',
'action' => 'index',
'class' => 'menu',
'pages' => array(
array(
'label' => 'Neu',
'id' => 'article',
'controller' => 'Article_New',
'action' => 'index',
'class' => 'submenu'
)
)
).....
And the code from the layout.phtml. On this code I show if the breadcump is shown or not.
foreach($container as $page) {
$par = $active;
if ($page != $active && $page->getClass()!='menu' && $page->getClass()!='menuF' && $page->getID()!=$par->getID()) {
$page->setClass('submenu');
} else {
$found = $container->findAllBy('ID', $active->getID());
foreach($found AS $p){
if($p->getClass()!='menu'&&$p->getClass()!='menuF'){
$p->setClass('sub');
}
}
}
}
echo $this->navigation()->menu()->renderMenu($this->nav);
Hope anybody can help me!
Thanks for all!
Best regards
Tom
I found myself the mistake!
I want to call something like this:
www.website/article/344435/23452345
surely this throws a mistake, because there isnt any action like 344435
with something like this
www.website/article/show/var1/2435234/var2/2345234352
its ok.
the rest is something for routing!
Big sorry to all!
Greats Tom
Try to use the route name instead of the module, controler and action system.
array(
'label' => 'Article',
'route' => 'Article_List',
'id' => 'article',
'class' => 'menu',
...
)
The router will handle all the URL parsing and return the correct variables.