A simple redirect component

I already wrote about doing a redirect with Ajax. Now, I created a simple component from that code I presented there:

// app/controllers/components/redirect.php
class RedirectComponent extends Object
    var $controller;
    var $components = array('RequestHandler');
    function startup(&$controller)
        $this->controller =& $controller;
    function goto($url)
        if ($this->RequestHandler->isAjax())
            $this->controller->set('url', $url);

The usage is easy. Add the component to the components array:

var $components = array('Redirect');

Afterwards you can use it in the following way:


Do not forget do add the code snippet to your view which necessary to do the javascript redirect:

<?php if (isset($url)) echo $javascript->codeBlock('window.location = "'.$url.'"'); ?>

Update (2006-04-03): Small bug fixed in startup function.



  1. Nao
    Posted March 28, 2006 at 5:00 pm | Permalink

    I don’t understand useful of “startup” method. Can you give me exemple ?

  2. Nao
    Posted March 28, 2006 at 5:17 pm | Permalink

    We must call it manually before redirect, it right ?

  3. Posted March 28, 2006 at 5:32 pm | Permalink

    The startup function is automatically called by Cake, you do not have to call it yourself.

Post a Comment

Required fields are marked *

%d bloggers like this: