A simple redirect component

Published on and tagged with ajax  cakephp  component  javascript

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);
        }
        else
        {
            $this->controller->redirect($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:

$this->Redirect->goto('/mycontroller/myaction');

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 = '.json_encode($url)); ?>

Update (2006-04-03): Small bug fixed in startup function.
Update (2009-12-05): Using json_encode to prevent a possible javascript injection problem. Thanks to pb for the hint.

11 comments baked

  • Nao

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

  • Nao

    We must call it manually before redirect, it right ?

  • cakebaker

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

  • nnc

    If all you want to do is redirect, I find it much more convenient to eliminate if (isset($url)) from views. To do that, I modified goto function to following:

    function goto($url)
    {
    if ($this->RequestHandler->isAjax()) {
    echo ”;
    echo ‘window.location = “‘.$url.'”‘;
    echo ”;
    exit();
    } else {
    $this->controller->redirect($url);
    }
    }

    I use this in admin section to redirect to login form if session has expired.

    Thanks for this great site cakebaker.

  • cakebaker

    @nnc: Thanks for the addition.

  • Aaron

    When I run this code, I get the following error:

    Fatal error: Call to a member function redirect() on a non-object in /path/to/app/controllers/components/component.php on line 18

    Line 18, in my case is:

    $this->controller->redirect(‘/users/login’);

    Any ideas? I’m pulling my hair out.

  • cakebaker

    @Aaron: Hm, strange, it works here. It looks like the controller is not set. Does the startup() method get called?

  • Shaun

    @nnc: Thanks for the simplification. I was planning on putting the javascript code at the top of all my layouts until I read your comment.

    @cakebaker: Thanks for the original idea. I haven’t been able to find any server-side solutions that work correctly.

  • cakebaker

    @Shaun: You are welcome!

  • pb

    Hi

    To prevent any possible javascript injection problems
    escape the url with json_encode
    This way, you’re sure the $url won’t break the javascript syntax

    echo ‘window.location = ‘.json_encode($url);

    Cheers

  • cakebaker

    @pb: Thanks for the hint, I fixed it in the article.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License