开发者

Cakephp getting session variable in model without using Auth component

开发者 https://www.devze.com 2022-12-18 01:05 出处:网络
I want to get my session variables in my model. Im not using Aut开发者_运维百科h component. Is there any alternate way ?

I want to get my session variables in my model. Im not using Aut开发者_运维百科h component. Is there any alternate way ?

Thanks Binoy


You would need to use the Session helper to do,

$this->Session->write('key','value');

But as the comment states, you'll be wanting to set a variable in your model and then use the session to write the same value into that variable in the model, rather than accessing the session actually in the model.

Class MyModel Extends AppModel{
  var $username;
  var $password;
}

Then in your controller you could use something along the lines of,

$this->MyModel->username = $this->Session->read('User.id');
$this->MyModel->password = $this->Session->read('User.password');


Controller File : use Session helper

class AppController extends Controller {
     var $helpers = array('Session');
     function index()
     {
        $this->Session->write('answer', '10'); //$this->Session->write('key','value');
     }
     ....
 }

Now you can access this varibale in your model :

 class AppModel extends Model {

    function validateanswer(){
        CakeSession::read('answer');/*Here u can get value of varibale answer which you set through controller*/
    }
 }
0

精彩评论

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

关注公众号