【问题标题】:How to define a variable and dynamically link to a URL in CakePHP?如何在 CakePHP 中定义变量并动态链接到 URL?
【发布时间】:2015-05-21 21:40:01
【问题描述】:

我正在尝试从 cakephp 扩展到“博客教程”,但在链接到登录用户主页时遇到了一些问题,我在名为 view.ctp 的文件中创建了该主页。

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

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

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

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

我该怎么做呢?我应该在哪里创建变量?*

var 是放在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'));
    }

}

?>

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    关注the blog tutorial text

    动作中的单条指令使用 set() 从 控制器到视图(我们将在接下来创建)。该行设置 名为“posts”的视图变量等于 Post 模型的 find('all') 方法。

    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 非常详细。

    【讨论】:

    • 文档对我来说有多详细并不重要。来自平面设计的背景,我是一个视觉学习者,我必须通过做一些事情来学习。我不会通过阅读轻松掌握逻辑。不过还是谢谢。
    • 好吧,那你有一个严重的问题。除了流程图和 DB 图,您在编程世界中找不到太多可以直观地学习的东西。你可以试试UML。 ;) 根据我在 Stackoverflow 上的经验,对于很多人来说,简单地仔细阅读文档是一个非常常见的问题。您需要首先学习使用结构化文本信息。此外,文档还附带大量示例代码,可以简单地复制和粘贴,以供不能或不会阅读文档的人试错。但恕我直言,首先了解基本概念至关重要。
    • 我完全同意,在一个完美的世界里,我会出去找一门关于它的课程。但是这个世界并不完美,我被带入一个项目来帮助工作中的同事,并且有学习时间限制,所以我写下了我需要知道如何做的事情,并一直专注于学习.
    • 然后进行在线培训。 training.cakephp.org 会有人与您和其他一些参与者共享他的屏幕 300 分钟,您可以提出问题并查看他在做什么。
    • 那太好了,我会把那个链接发给 CTO 并尝试获得报酬。至于现在,我想我会再次重做博客教程。尝试了解正在发生的一切。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2010-09-29
    相关资源
    最近更新 更多