tags:

views:

42

answers:

1

I'm using CakePHP and I have a component I wrote that uses a component called "Users" to handle users login, logout, registration, etc. I would like to render that on the default.ctp layout in a sidebar. How can I do this?

I tried:

    <div id="leftNav">
        <div id="login-block" class="block">
            <?php echo $this->element('loginblock', array('component' => 'user')); ?>
            <ul>
                <li><a href="/users/login">Login</a></li>
                <li><a href="/users/register">Register</a></li>
                <li><a href="/users/logout">Logout</a></li>
            </ul>
        </div>
    </div>

but that was unsuccessful.

A: 

You cannot, by definition, render a component. Only the V part of the MVC architecture can be rendered (or at least should be), and components belong in the C part.

The best way to pass information from a component to a view is by setting it via the controller like this:

class MyComponent extends Object {

    function initialize(&$controller) {
        $this->controller =& $controller;
    }

    function someMethod() {
        $this->controller->set('user', $someInformation);
    }

}

And in the view you can output the $user variable as usual.

deceze
but the user component is only active on http://url/users/* pages. I want to be able to render it's view (login.ctp or logout.ctp) on every page. I.E. in the defualt.ctp
Malfist
@Malfist How would you render its view if you don't include the component? If you want to display content based on information that is determined in the User component, then that component will need to run every time. You **could** use `requestAction` in the view, but this will still need to start up the component to get the information. One way or another, you'll need to include the component everywhere if you want its information. As an alternative though, you can store the information in the Session, if it's okay that they're not being updated.
deceze