Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cakephp internal redirect from controller to another controller

Update: i wrote some wrong statements about the use of header in php; so forget that part :)

What i want is to fetch and display a controller's view (with controller's data) from another controller, without have url change in browser.

Some details:

  1. Redirect doesn't do the job because is a direct redirect (via browser)
  2. requestAction doesn't allow me to fetch css and images correctly

I need this thing because i have a controller dispatcher that redirects internally to the other controllers.

I think the only (correct) solution is to use routes.php in /config with Router::connect and there use the logic that was in the dispatcher controller.

like image 932
apelliciari Avatar asked Dec 03 '22 03:12

apelliciari


1 Answers

ummm... header() is the function to use for a redirect unless the PHP documentation is wrong. (http://php.net/manual/en/function.header.php) The core in cakePHP uses header for the redirect function (see lines 721 - 730 of cake/libs/controller.php).

So I am not certain what you mean "like normal PHP". CakePHP is PHP, it's just built on object oriented code. It's not magic or twisted ways of doing things. So to do a redirect in cake, you can simply use:

$this->redirect(array('controller' => 'my_controller', 'action' => 'my_action'));

And it will call the header() function.

Now. If you are dead set on not using redirect (maybe if you are going to an external site), you can call header() in the code. Just be sure you put the exit(); after the header call:

header('Location: http://call/my/url');
exit();

It will work just the same as redirect. It's just a lot of unnecessary extra work. Keep in mind that using redirect will maintain the domain name and build the URL for you automatically.

like image 115
Chuck Burgess Avatar answered Dec 21 '22 18:12

Chuck Burgess