如何在CakePHP中定义变量并动态链接到URL?

时间:2015-05-21 12:59:15

标签: php cakephp

我正在努力扩展到博客教程'来自cakephp并且在链接到登录用户的主页时遇到一些麻烦,我在一个名为view.ctp的文件上创建了该主页。

我可以链接到我的大部分文件路径http://localhost:8888/blogtest/users/view/,直到我需要' id'定义发送某人的页面。

这是我链接到该页面的方式:

<a href="/blogtest/users/view/<?php ?>">go</a>

我知道我需要php标签中的一些逻辑来告诉浏览器检索当前登录用户的ID。

我将如何做到这一点?我在哪里创建一个变量?*

变量是UsersController.php还是User.php?非常感谢任何帮助!

UsersController php:

<?php

// app/Controller/UsersController.php
App::uses('AppController', 'Controller');



class UsersController extends AppController {

    public function beforeFilter() {
    parent::beforeFilter();
    // Allow users to register and logout.
    $this->Auth->allow('add', 'logout');
    }

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
    }

    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        }
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(
                __('The user could not be saved. Please, try again.')
            );
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function delete($id = null) {
        // Prior to 2.5 use
        // $this->request->onlyAllow('post');

        $this->request->allowMethod('post');

        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('User deleted'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('User was not deleted'));
        return $this->redirect(array('action' => 'index'));
    }

}

?>

1 个答案:

答案 0 :(得分:1)

注意the blog tutorial text

  

动作中的单个指令使用set()从中传递数据   控制器到视图(我们接下来会创建)。这条线设置了   视图变量名为'posts'等于的返回值   找到Post模型的(&#39; all&#39;)方法。

It goes on here并解释完全您想要的内容:

        <td>
            <?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
        </td>

老实说,我怀疑你试图读它。如果你真的再次阅读本节。 It is all there非常详细。