Why are you doing it that way and not just using the Auth component?
Auth component basically magics up all of that stuff for you....

http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html

point that at a table with a username and password field and you're set
(more or less).

I'd suggest perhaps that the security component isn't being turned on by
the controller generating the home page - where it looks like it is on the
users controller... users controller is expecting the security bits which
are not available at the time the form is being generated.



On Mon, Sep 10, 2012 at 11:11 AM, shyandsy <shyan...@gmail.com> wrote:

> Pretty sure!
>
> home.ctp
> ....................
> <?php
> echo $this->element('LoginForm');
> ?>
> .....................
>
> element/LoginForm.ctp
> <?php
> echo $this->Form->create('User', array('url' => array('controller' =>
> 'users', 'action' => 'login')));
> echo $this->Form->input('username');
> echo $this->Form->input('password');
> echo $this->Form->end('登录系统');
> ?>
>
>
> UsersController.php
>
> public function login()
> {
> if($this->request->is('post'))
> {
> $result = $this->User->findByUsernameAndPassword(
> $this->data['User']['username'],
> $this->data['User']['password']);
> if($result)
> {
> $credential = Security::generateAuthKey();
>  //将凭据更新到数据库
> $this->request->data['User']['credential'] = $credential;
> $this->User->id = $result['User']['id'];
> $this->User->save($this->request->data);
>  $this->Session->setFlash('登录认证成功');
>  //设置cookie
> $this->Cookie->write('Users.username', $this->data['User']['username']);
> $this->Cookie->write('Users.credential', $credential);
> $this->redirect(array('action'=>'index'));
> }
> else
> {
> $this->Session->setFlash('登录认证失败');
> }
> }
>  }
>
> 在 2012年9月9日星期日UTC-5下午6时40分34秒,Greg写道:
>>
>> can you post the code?
>>
>>
>> On Mon, Sep 10, 2012 at 1:45 AM, shyandsy <shya...@gmail.com> wrote:
>>
>>> It doesn't work.
>>> The submit can not find the controller.
>>>
>>> I compared the different between 127.0.0.1/user/login and  127.0.0.1 .
>>>
>>> This is the login form in the login page:
>>> <form action="/users/login" id="UserLoginForm" method="post"
>>> accept-charset="utf-8">
>>>  <div style="display:none;">
>>> <input type="hidden" name="_method" value="POST"/>
>>>  <input type="hidden" name="data[_Token][key]" value="
>>> f9da31eb2cd89c4e9c10af7**156a36f6bc9ae7fdd" id="Token1312017746"/>
>>>  </div>
>>> <div class="input text required">
>>>  <label for="UserUsername">Username</**label>
>>>  <input name="data[User][username]" maxlength="40" type="text" id="
>>> UserUsername"/>
>>>  </div>
>>> <div class="input password required">
>>>  <label for="UserPassword">Password</**label>
>>>  <input name="data[User][password]" type="password" id="UserPassword"/>
>>>  </div>
>>> <div class="submit">
>>>  <input type="submit" value="登录系统"/>
>>> </div>
>>>  <div style="display:none;">
>>> <input type="hidden" name="data[_Token][fields]" value="
>>> e44df0d754a25ad586a8cb2**0d5d0ead3d4e78843%3A" id="TokenFields287192969
>>> "/>
>>>  <input type="hidden" name="data[_Token][unlocked]" value="" id="
>>> TokenUnlocked1990315235"/>
>>>  </div>
>>> </form>
>>>
>>> This is the login form element in the homepage
>>> <form action="/users/login" id="displayForm" method="post"
>>> accept-charset="utf-8">
>>>  <div style="display:none;">
>>> <input type="hidden" name="_method" value="POST"/>
>>>  </div>
>>> <div class="input text">
>>>  <label for="username">Username</**label>
>>>  <input name="data[username]" type="text" id="username"/>
>>>  </div>
>>> <div class="input password">
>>>  <label for="password">Password</**label>
>>>  <input name="data[password]" type="password" id="password"/>
>>>  </div>
>>> <div class="submit">
>>>  <input type="submit" value="login"/>
>>>  </div>
>>> </form>
>>>
>>> Obviously, The login form in user/login page has been assign some staff
>>> like token, but element is not.
>>>
>>> 在 2012年9月6日星期四UTC-5下午7时10分03秒,**Greg写道:
>>>>
>>>> Thats fine.
>>>>
>>>> The solution for using elements is fairly straight forward. Build your
>>>> Controller and your model *as normal* - so your Users controller has a
>>>> login method.
>>>>
>>>> Next, create a new element under View/Elements called "LoginForm.ctp".
>>>>
>>>> The login form would look something like this:
>>>>
>>>> <php
>>>>
>>>> echo $this->Form->create(null, array('url' => array('controller' =>
>>>> 'Users', 'action' => 'login')));
>>>> // ... rest of the form
>>>> echo $this->Form->end('login');
>>>> ?>
>>>>
>>>> back over in your home.ctp file you put this where you want the login
>>>> form to be visible:
>>>>
>>>> <?php
>>>> echo $this->element('LoginForm');
>>>> ?>
>>>>
>>>> and you're done - the home page will now have the content from
>>>> LoginForm.ctp which will correctly submit the form data to the login action
>>>> on the Users controller.
>>>>
>>>> You can of course then reuse the LoginForm element anywhere you like -
>>>> say the user navigates directly to /Users/login you could dump the login
>>>> form element on that page. Or say you wanted it in the header for every
>>>> page, modify the layout to include the element.
>>>>
>>>> You don't *have* to associate every action with a specific view for
>>>> that controller/action combination - it is often quite beneficial to drop
>>>> reusable forms into elements so that you can reuse them over and over
>>>> again. It helps with maintenance as well because if you modify the form
>>>> element, you are modifying it in all places in your site at once instead of
>>>> having to go and modify every template/view that contains it.
>>>>
>>>>
>>>> On Fri, Sep 7, 2012 at 8:08 AM, shyandsy <shya...@gmail.com> wrote:
>>>>
>>>>> Got it, I already know the difference between reply on here and reply
>>>>> to your mailbox.
>>>>>
>>>>>
>>>>> 在 2012年9月5日星期三UTC-5上午2时45分34秒,**Gr**eg写道:
>>>>>
>>>>>> You don't use a view. Make an element to start with under
>>>>>> views/elements.
>>>>>> Call it loginForm.ctp or something.
>>>>>>
>>>>>> Then define an action in the form helper create call to point it at
>>>>>> the right controller/action
>>>>>>
>>>>>> On Wednesday, September 5, 2012, shyandsy wrote:
>>>>>>
>>>>>>> Hi,  thanks for your anwser firstly.
>>>>>>>
>>>>>>> I have tried the method of element.
>>>>>>> I can load the login view that were written in the
>>>>>>> view/users/login.ctp,
>>>>>>> but I do not know how to process the POST request by
>>>>>>> UsersController.login()?
>>>>>>>
>>>>>>> I can not find the detail description on the cookbook 2.0.
>>>>>>>
>>>>>>>
>>>>>>> 在 2012年9月4日星期二UTC-5下午7时09分33秒,**Gr****eg写道:
>>>>>>>>
>>>>>>>> I'd do it with an element personally. Have the home page retrieve
>>>>>>>> the data from the models in question then pass the resulting data to
>>>>>>>> individual elements...
>>>>>>>>
>>>>>>>> requestAction is a major performance hit, especially if you are not
>>>>>>>> caching the results...and on the home page as described here you are
>>>>>>>> looking at subjecting your users to 3 calls to the dispatcher (one for
>>>>>>>> pages, one for news, one for users).
>>>>>>>>
>>>>>>>> You *can* use requestAction and elements together, then cache the
>>>>>>>> element to reduce the hit if you wish.
>>>>>>>>
>>>>>>>>
>>>>>>>> On Wed, Sep 5, 2012 at 5:06 AM, Mark Wratten <ma...@trimar.com>wrote:
>>>>>>>>
>>>>>>>>> I am assuming you have a route that routes to a specific
>>>>>>>>> controller/action, e.g. pages controller, index action.
>>>>>>>>> Then in you home page view View/Pages/index.ctp -
>>>>>>>>>
>>>>>>>>> <p>Welcome to my home page.</p>
>>>>>>>>>
>>>>>>>>> <div id="news-summary">
>>>>>>>>>   <h2>News Summary></h2>
>>>>>>>>>   <?php echo $this->requestAction('/news/**su******mmary'); ?>
>>>>>>>>> </div>
>>>>>>>>>
>>>>>>>>> <div id="login-form">
>>>>>>>>>   <?php echo $this->requestAction('users/**lo******gin'); ?>
>>>>>>>>> </div>
>>>>>>>>>
>>>>>>>>> Then, in your News controller, create and action - summary, and a
>>>>>>>>> view file summary.ctp.
>>>>>>>>> In your Users controller create action login (which should also
>>>>>>>>> handle the POST request when the user logs in) and a login.ctp view 
>>>>>>>>> file.
>>>>>>>>>
>>>>>>>>> Then when the home page is rendered, the news summary and login
>>>>>>>>> forms will be displayed.
>>>>>>>>>
>>>>>>>>> Mark
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Monday, September 3, 2012 8:04:34 PM UTC-4, shyandsy wrote:
>>>>>>>>>>
>>>>>>>>>> could you put some details on, thanks a lot!
>>>>>>>>>> Making a home page that includes login and news list.
>>>>>>>>>>
>>>>>>>>>> I can write a view and controller for that job login or news
>>>>>>>>>> list, but I don't know how to make that things togerther!!
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> 在 2012年9月3日星期一UTC-5上午12时22分41秒,**M********ark Wratten写道:
>>>>>>>>>>>
>>>>>>>>>>> Easiest is to use requestAction() in the homepage view.
>>>>>>>>>>>
>>>>>>>>>>> On Thursday, August 30, 2012 4:26:12 PM UTC-4, shyandsy wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> I am kind of new guy on cakePhp. So the description as below:
>>>>>>>>>>>>
>>>>>>>>>>>> I hope to provide two function to users in the homepage, login
>>>>>>>>>>>> and news.
>>>>>>>>>>>> The login uses the view login and the controller
>>>>>>>>>>>> loginController, and the news uses the news view and 
>>>>>>>>>>>> newscontroller.
>>>>>>>>>>>> The problem is how to put the two view in the home page.
>>>>>>>>>>>>
>>>>>>>>>>>> (apologize, my first language is not English, if you meet some
>>>>>>>>>>>> problem about grammar, I will try to fix it)
>>>>>>>>>>>>
>>>>>>>>>>>  --
>>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>>> Groups "CakePHP" group.
>>>>>>>>> To post to this group, send email to cake...@googlegroups.com.
>>>>>>>>> To unsubscribe from this group, send email to cake-php+u...@**
>>>>>>>>> googlegroups.com******.
>>>>>>>>> Visit this group at http://groups.google.com/**group******
>>>>>>>>> /cake-php?hl=en-US<http://groups.google.com/group/cake-php?hl=en-US>
>>>>>>>>> .
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>  --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "CakePHP" group.
>>>>>>> To post to this group, send email to cake-php@googlegroups.com.
>>>>>>> To unsubscribe from this group, send email to cake-php+unsubscribe@*
>>>>>>> *googlegro****ups.com.
>>>>>>> Visit this group at http://groups.google.com/**group****
>>>>>>> /cake-php?hl=en-US<http://groups.google.com/group/cake-php?hl=en-US>
>>>>>>> .
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>  --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "CakePHP" group.
>>>>> To post to this group, send email to cake...@googlegroups.com.
>>>>> To unsubscribe from this group, send email to cake-php+u...@**
>>>>> googlegroups.com**.
>>>>> Visit this group at http://groups.google.com/**group**
>>>>> /cake-php?hl=en-US <http://groups.google.com/group/cake-php?hl=en-US>.
>>>>>
>>>>>
>>>>>
>>>>
>>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "CakePHP" group.
>>> To post to this group, send email to cake...@googlegroups.com.
>>> To unsubscribe from this group, send email to cake-php+u...@**
>>> googlegroups.com.
>>> Visit this group at 
>>> http://groups.google.com/**group/cake-php?hl=en-US<http://groups.google.com/group/cake-php?hl=en-US>
>>> .
>>>
>>>
>>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.


Reply via email to