Zend Framework multi-word action names - php

This question has been asked many times but for some reason the proposed solution isn't working for me.
I wan't to use names like "deleteDefaultUser" for my action. To achieve this I have done following.
Added a route
$route = new Zend_Controller_Router_Route_Static(
'user/delete-default-user',
array(
'action' => 'deleteDefaultUser',
'controller' => 'user',
'module' => 'root'
)
);
$router->addRoute('delete-default-user', $route);
Defined my action as below
public function deleteDefaultUserAction(){
//some code
}
And generated a URL like this
echo $this->url(array(), 'delete-default-user');
( This generates the URL /user/delete-default-user)
But for some reason I am still getting the error shown below:
Zend_Controller_Action_Exception: Action "deletedefaultuser" does not exist and was not trapped in __call() in C:\Users\Jay\Projects\EOP\library\Zend\Controller\Action.php on line 485
I have checked the controller and action names are correct. But from the error message it seems that Zend Framework is not applying camel case to action names.
The version of Zend Framework that I am using is 1.12.
Can any one please help with this?
Edit: If I change my action name to 'deletedefaultuser' it works correctly.

Change the action part of the route to:
'action' => 'delete-default-user',
and then it should work.

Related

Basic Kohana Routing

I'm new with Kohana and finding their documentation to be lacking (lots of incomplete writings, lots of broken links, etc.). I just want to create a route like so:
Route::set('test1', 'blah/<id>')
->defaults(array(
'controller' => 'Blah',
'action' => 'foo',
));
So if the URL is localhost/blah/8342342 it will run through this controller and action. The problem is I get a 404 error. If I change the URI in the Route::set to be blah/foo/<id> it works fine. I only want the /blah/ directory though, not 'blah/foo'. Is this possible or do you need to have both the controller and action in the URL?
Another question, does the first directory in your URI (in this case /blah) HAVE to match the controller name? For example, if the first directory in the URI is "blah/", does that mean my controller must be named "Blah.php"? From my tests it seems that this is the case, but I don't know why it would be set up that way. What if I wanted the URI "contact/" to go through controller Blah?

Getting parameter from url in Zend

My route setup :
Zend_Controller_Front::getInstance()
->getRouter()
->addRoute('view', new Zend_Controller_Router_Route('controller/action/:name'))
My link in view :
$this->url(array("name" => "John"), "view", TRUE);
// returns "controller/action/John" as should
Now, when I am at controller/action/John, how do I get the name from URL ? I tried
$this->getRequest()->getParam("name");
but the name param isn't there - getRequest() returns only controller, action and module params.
When you set up your route configuration the route definition should either directly match the controller/action names or be set with defaults. Actually setting the defaults in any case is just good practice and helps you avoid issues like this.
So, in your case according to the comments your route should probably look like this.
$defaults = array(
'controller'=> 'offers',
'action' => 'view',
'name' => ''
);
$route = new Zend_Controller_Router_Route('offers/view/:name',$defaults);
As mentioned in the comments you can always check what route has been used with Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName(). If it doesn't show your expected route the Router isn't able to find a match and moves on until it usually ends in the "default" route.
As a side note to your question: When you use $this->url(array("name" => "John"), "view", TRUE) you only create the link based on the route. This method is only part of the view and does nothing in terms of dispatching to a controller or action.
For those who found this question and for future reference, you can get params from route using this : $this->params()->fromRoute('param1', 0); in Zend Framework 2 at least. This is what I was looking for in this question.

CakePHP - routing with wildcards (controller not found error)

I'm not really even sure how exactly to search for this but I have a URL
site.com/forum/controller/action
Where forum is a plugin and I currently have it routing to the plugin forum successfully with
Router::connect('/forum', array('plugin' => 'forum', 'controller' => 'home', 'action' => 'index'));
However, I want to add a route that will connect any top-level subdirectory to the plugin forum. For example,
site.com/fish/controller/action
site.com/bird/controller/action
would both route to the forum plugin. Similarly,
site.com/bird
would also route to the forum plugin. This is the best I have been able to come up with and it has no effect (I get a "FishController could not be found":
Router::connect('/*/:controller/:action/*', array('plugin' => 'forum'));
The closest answer I could find basically says this might not be possible? http://cakephp.1045679.n5.nabble.com/Routes-with-wildcards-missing-controller-errors-td1263632.html
EDIT: After some more trial & error I tried this:
Router::connect('/:site/:controller/:action/*', array('plugin' => 'forum'));
And it works. Could someone explain this?
The documentation at http://api.cakephp.org/class/router#method-Routerconnect does a great job at explaining this.
What you've done is created a custom parameter. Cake uses an array to keep track of the parameters and that how it know which controller, action and other parameters have been passed. The Router will convert any URLs with 3 slashes (/) to $param['site'], $param['controller'] and $param['action'].
From your controller, you can retrieve the value of :site by using $this->params['site'].

Turn off default routes in Kohana 3?

I believe I know how to do this, but wanted to verify with my awesome community peeps. =)
Here's an example:
I have a Controller class called 'tami', with an action 'index'.
I know that if I want someone to access that controller/action combo via an URL other than "/tami/" or "/tami/index", then I should add a route, via something like this:
Route::set('pretty_tami', 'these-are-my-initials(/<action>)')
->defaults(array(
'controller' => 'tami',
'action' => 'index',
));
But, users can still access this page via /tami/.
How can I turn off the default routing, so that the only valid routes are the ones I define?
I assume I can just remove the default route found in kohana/application/bootstrap.php. Is that correct? Or would that break something else?
I'd say exactly the same as #simshaun — either remove the default route (leaving other controllers unreachable) or check in the before() function in Controller_Tami for the uri to see if it's what you're after.
If you're using Kohana 3.1, you can now use lambda logic/anonymous functions to define your routes.
Something like this would take the extra routing logic out of the controller (which is good as we're keeping it in one place):
Route::set('default', function($uri)
{
if ($uri == 'tami' OR $uri == 'tami/index')
{
// Route not allowed by the default methods
throw new Kohana_404_Exception("Route not permitted");
}
},
'(<controller>(/<action>(/<id>)))'
);
Something I haven't yet used but it looks amazingly powerful.
I think the easiest way would be to remove the default route in your bootstrap file, yes. However, any controllers that you have not manually specified a route for can no longer be accessed.
What I would do is create a class, e.g. Controller_Derouter that Controller_Tami extends. Use the before() method in Controller_Derouter to test if the controller was accessed from the default route, and if so, throw a 404. I think you should be able to do that by comparing $this->request->controller against the first URI segment.
Edit: The solution mentioned above is unnecessary if you ever only plan on disabling the default route for just the Tami controller. If that's the case, you could just implement the before() method directly in the Tami controller.
Maybe like this?
Route::set('pretty_tami', 'these-are-my-initials/<action>')
->defaults(array(
'controller' => 'tami',
));
So there wouldn't be a default action. And you probably want to update the default route (if you still have one) with a regex to exclude tami.
Route::set('default', '(<controller>(/<action>(/<id>)))', array('controller' => '/^(?!tami)/'))
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));

CakePHP 1.3.7 inverse routing returning default url, instead of the configured one

Im developing a simple application with CakePHP v1.3.7 Stable. I want to generate a simple user profile page, accessible by the url: my.domain/u/id, where id is the id of the user in database.
So i wrote this (and only this) in app/config/routes.php:
Router::connect('/u/:id',
array('controller' => 'Users', 'action' => 'profile')
,array('pass'=>array('id'),'id'=>'[0-9]+')
);
The above code works fine, when i put my.domain/u/120 in the browser, it shows the profile of user 120.
But, when i try to create a link to this page using the Html helper:
// some code in a view
$this->html->link('Test', array('controller'=>'Users', 'action'=>'profile', 120))
The html helper (doing inverse routing, i think) generates the url in the defaut cakephp form: Test
Based on the configuration in routes.php, it should be: Test, right?
I'm missing something?
Thanks.
Try with this:
$this->html->link('Test', array('controller'=>'Users', 'action'=>'profile', 'id'=>120))
I hope the missed 'id' will fix it.
Finally, thanks to dogmatic69, I deleted the id related code of the route.
I end with this in routes.php:
Router::connect('/u/*',
array('controller' => 'users', 'action' => 'profile')
);
Now, the html helper works just as expected:
echo $this->html->link('Test',array(
'controller'=>'users',
'action'=>'profile',
100
))
// renders: Test
The drawback is that I can't take advantage of the regexp filter of the router, as in the initial route code.

Categories