开发者

CakePHP - How to use login form as Element

开发者 https://www.devze.com 2023-03-14 02:35 出处:网络
I am learning CakePHP. I am trying to develop a login system, but instead of using the login.ctp as a user/view, I wish I could use it as a element because I have the login form in many other pages. H

I am learning CakePHP. I am trying to develop a login system, but instead of using the login.ctp as a user/view, I wish I could use it as a element because I have the login form in many other pages. How can I declare within my users_controller that my function login() will use just the element. I said that because I used $this->render('/elements/login') and it did work. However, my element login.ctp was within my default.ctp layout, thus, I had two login forms. One was the element and the other as my con开发者_StackOverflow社区tent in my default.ctp layout.

Thanks!


Layouts are for the "greater" markup of a page: head and meta information, includes, "footer" stuff like your analytics. Generic content pointers go in the middle - so there's no need to mark anything as specific as a form in a layout unless you really do want to include that form on every page that uses that layout.

It sounds like you either called $this->render() in your view? $this->render() is a controller method. $this->element is the view method.

Or you called $this->element('/elements/login'); from in your login.ctp view? That would mean the controlller rendered the default login.ctp view, which called the login.ctp element.

And thus you saw two. To fix:

To return something other than the default view associated with an action (such as your login snippet), call $this->render('name/of/whatever'); as the last line of the controller method. It will return the view you specify; set will pass whatever variables to it, just like a regular view call and if you want to get fancy, specify the layout as ajax and watch the magic start like $this->render('/elements/login', 'ajax').

If you need to call several elements in a single view file, use the method $this->element('/fancy/nav/whatever'); you can also place them in layouts as appropriate (navigation, etc.)

HTH. :)


I had the same thing done to my project and this is what I did.

Basically, I created a new loginElement.ctp and placed it in the element folder. I create new sets of HTML code that would fit the layout where I wanted to use this element and the loginElement.ctp <form would then submit the data to login action in the users_controller.

If you need to and when I get home later, I can post my exact code here.

==================================== EDIT =========================================

These are the codes I used:

First of all you will notice that the action in the login form points to /login.

I have that setup in my /config/routes.php file as such

Router::connect('/login', array('controller' => 'users', 'action' => 'login'));

Then the other codes are below

/views/elements/thinlogin.ctp

<div id="login">                
    <form method="post" action="/login" accept-charset="utf-8" class="formBox">
      <fieldset>
            <input type="hidden" name="_method" value="POST" />
        <div class="form-col">
            <label for="username" class="lab">Username/Email</label>
            <input name="data[User][username]" type="text" id="UserUsername" class="input">
        </div>
        <div class="form-col form-col-right">
            <label for="password" class="lab">Password</label>
            <input type="password" name="data[User][password]" id="UserPassword" class="input">
        </div>
        <div class="form-col form-col-submit"> 
          <input name="" value="Login" class="submit" type="submit">
        </div>     
        <div class="form-col form-col-check">
          <label><input name="remember" class="checkbox" type="checkbox">Remember me on this computer</label>
        </div>            
      </fieldset>
    </form>
</div>

/views/pages/home.ctp

<div id="home_top_right_top">
        <?php
        if (!$this->Session->check('Auth.User.id'))
        {
            echo $this->element('login/thinlogin');
        }else{
            echo $this->element('login/loggedin');
        }
        ?>        
</div>
0

精彩评论

暂无评论...
验证码 换一张
取 消