在cakephp中应用会话3.2

时间:2016-04-21 11:48:06

标签: cakephp

我使用cakephp 3.2来构建应用程序。我使用书签教程作为我的项目的基础。在我的一个书签.ctp视图文件中我想有一些选择框,其中包含特定于用户登录的数据。我有两个表,即用户和书签。我的书签表包含来自用户表user_id的外键。 这是我的书签表,其中包含我想要的下拉菜单字段。 id,user_id,title,systemregistration,systemroles,country,province,metropolitan。

Code for my appcontroller
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller
{

    /**
     * Initialization hook method.
     *
     * Use this method to add common initialization code like loading components.
     *
     * e.g. `$this->loadComponent('Security');`
     *
     * @return void
     */

    /*public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
    }*/

    public function initialize()
    {
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'email',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
            ],
            //'storage' => 'Session'
            'Session'
        ]);
        // Allow the display action so our pages controller
        // continues to work.
        $this->Auth->allow(['display']);
    }

    /*public function initialize()
    {
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Bookmarks',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'display',
                'home'
            ]
        ]);
    }

    public function beforeFilter(Event $event)
    {
        $this->Auth->allow(['index', 'view', 'display']);
    }*/

    /**
     * Before render callback.
     *
     * @param \Cake\Event\Event $event The beforeRender event.
     * @return void
     */
    public function beforeRender(Event $event)
    {
        if (!array_key_exists('_serialize', $this->viewVars) &&
            in_array($this->response->type(), ['application/json', 'application/xml'])
        ) {
            $this->set('_serialize', true);
        }
    }
}

//BookmarksController looks like this

namespace App\Controller;

use App\Controller\AppController;


/**
 * Bookmarks Controller
 *
 * @property \App\Model\Table\BookmarksTable $Bookmarks
 */
class BookmarksController extends AppController
{
    public function internalprotocol()
    {

        $bookmark = $this->Bookmarks->newEntity();

        $users = $this->Bookmarks->Users->find('list', ['limit' => 200]);
        $tags = $this->Bookmarks->Tags->find('list', ['limit' => 200]);
        $this->set(compact('bookmark', 'users', 'tags'));
        $this->set('_serialize', ['bookmark']);

        $bookmarks = $this->paginate($this->Bookmarks);

        $this->set(compact('bookmarks'));
        $this->set('_serialize', ['bookmarks']);        
    }
}

//my internalprotocol.ctp looks like this 

<div>
    <?php echo $this->Form->input('user_id', ['options' => $bookmarks]); ?>

    <?php echo $this->Form->input('title', ['options' => $bookmarks]); ?>

    <?php echo $this->Form->input('systemregistration', ['options' => $bookmarks]); ?>

    <?php echo $this->Form->input('systemroles', ['options' => $bookmarks]); ?>

    <?php echo $this->Form->input('country', ['options' => $bookmarks]); ?>
</div>

我想用特定于用户登录的数据填充每个字段。你能帮忙吗?

2 个答案:

答案 0 :(得分:1)

你不需要做任何事情。如果登录成功,您可以使用Auth

通过$this->Auth->user();组件访问登录的用户详细信息

如果您需要向会话添加更多信息,可以使用Session组件$this->Session->write('User.AscociatedData', $AscociatedData);

在视图中访问此数据的最简单方法是将经过身份验证的用户设置为控制器中的视图变量:

$this->set('user',$this->Auth->user());

然后您可以使用$user访问视图中的用户信息,例如$user->fieldName

不完全确定你的要求,但我希望我的一个答案是相关的。

答案 1 :(得分:1)

we only need to show bookmarks for the currently logged in user.

We can do that by updating the call to paginate().Make your index() action from Controller/BookmarksController.php look like:

public function index()
 {
  $this->paginate = [
    'conditions' => [
        'Bookmarks.user_id' => $this->Auth->user('id'),
    ]
];
$this->set('bookmarks', $this->paginate($this->Bookmarks));
$this->set('_serialize', ['bookmarks']);

}

We should also update the tags() action and the related finder method as we done for bookmarks above

Please read the tutorial

http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/part-two.html#fixing-list-view-and-forms