views:

2056

answers:

2

Is there any other command for redirecting a controller to a particular view page other than redirect and render?

I have the redirect as

$this->redirect('/forms/homepage/'.$userId);

But if I give

$this->render('/forms/homepage/'.$userId);

it doesn't get redirected to that page.

Is something wrong?

+5  A: 

The call to redirect() issues a HTTP redirect. Nothing happens after the redirect because CakePHP simply stops. Anything you put after the redirect call will not be executed. Instead, the browser simply issues a new HTTP GET to the URL you are redirecting to.

The call to render() simply loads a view. It takes a path to a view, not an URL. It does not redirect. Assume that $userID is '101' in your case. The call to render() would try to load the following file:

app/views/forms/homepage/101.ctp

Since that file does not exist, nothing happens.

Sander Marechal
A: 

I think Sander is right, but I'm not 100% sure if the redirect will stop execution of the rest of the controller method. If you have something after the redirect it might still execute that and that may be causing your problem. If you do something like:

return $this->redirect('/forms/homepage/'.$userId);

that should stop the execution. Or maybe put an exit after the redirect.

jimiyash
By default the redirect()-method stops the script execution, see the API: http://api.cakephp.org/class/controller#method-Controllerredirect
dhofstet