【问题标题】:Generate link in cakephp using Html helper使用 Html 助手在 cakephp 中生成链接
【发布时间】:2015-02-16 16:35:56
【问题描述】:

我正在尝试使用 Html 助手在我的控制器中简单地创建一个链接,尽管我添加了必要的助手,但我收到以下错误:

在非对象上调用成员函数link()

 public $helpers = array('Html', 'Form');

   $url = $this->Html->link(
                            '',
                            'http://www.example.com/',
                            ['class' => 'button', 'target' => '_blank']
                        );

【问题讨论】:

    标签: html cakephp cakephp-2.3


    【解决方案1】:

    您可以在视图文件中使用 Helpers,但不能在控制器中使用 http://book.cakephp.org/2.0/en/views/helpers.html#using-helpers

    例如在你的 index.ctp

    echo $this->Html->link(
        __('My link'), 
        'http://www.example.com/', 
         array('class' => 'button', 'target' => '_blank')
    );
    

    在 Controller 中启用 Html Helper 与在代码中相同。

    class ExamplesController extends AppController {
        $helpers = array('Html', 'Form');
    
        public function index() {
           //
        }
    }
    

    【讨论】:

      【解决方案2】:

      这是个好问题。我认为您对 MVC 和设计模式提供的关注点分离有点困惑。 (再次)看看 CakePHP 如何实现 MVC:http://book.cakephp.org/2.0/en/cakephp-overview/understanding-model-view-controller.html

      要记住的重要一点是,您的控制器永远不应该关心创建anchor 标签。这就是你的观点的工作。由于助手是一种让您的视图保持干燥(不要重复自己)的方法,因此唯一的责任就是创建 HTML 元素非常方便。视图依赖于控制器来确定设置了哪些变量、它们的值是什么以及加载了哪些帮助程序。有关 Helpers 以及控制器组件和模型行为的更多信息,请查看 http://book.cakephp.org/2.0/en/getting-started/cakephp-structure.html 以及它们各自的文档页面:

      现在您对 MVC 有了更好的了解,让我们来看看您的具体问题。您想在控制器中创建链接。我认为它可能是动态的,取决于其他一些变量,所以我将继续使用它。

      您可以解决的一个常见问题是根据用户是否已登录来显示登录/注销链接。

      app/Controller/ExampleController.php

      class ExampleController extends AppController {
          public $components = array('Auth');
      
          public $helpers = array('Html', 'Form');
      
          public function beforeRender() {
             parent::beforeRender();
             //if Auth::user() returns `null` the user is not logged in.
             if ($this->Auth->user() != null) {
                 $logInOutText = 'Log out';
                 $logInOutUrl = array('controller' => 'users', 'action' => 'login');
             } else {
                 $logInOutText = 'Log in';
                 $logInOutUrl = array('controller' => 'users', 'action' => 'logout');
             }
             $this->set(compact('logInOutText', 'logInOutUrl'));
          }
      }
      

      然后,您可以在您的视图中做一些简单的事情。在这种情况下,我选择默认布局,因为我想要 每个 呈现页面中的链接。 app/View/Layouts/default.ctp

      <!-- More HTML above -->
      <?php
      // "Html" in `$this->Html` refers to our HtmlHelper. Note that in a view file
      // like a `.ctp`, `$this` referes to the View object, while above in the
      // controller `$this` refers to the Controller object. In the case
      // `$this->Html`, "Html" would refer to a component. The same goes for Models
      // and behaviors.
      echo $this->Html->link($logInOutText, $logInOutUrl); // Easy!
      ?>
      <!-- More HTML below -->
      

      我希望这会有所帮助。我知道一次要完成很多工作。

      【讨论】:

        【解决方案3】:

        虽然这不是一个好习惯。但是,如果你需要这个,你可以在你的控制器中使用以下代码

        App::uses('HtmlHelper', 'View/Helper');
                $yourTmpHtmlHelper = new HtmlHelper(new View());
                $url=$yourTmpHtmlHelper->link(
                                    '',
                                    'http://www.example.com/',
                                    ['class' => 'button', 'target' => '_blank']
                                );
        

        这应该适用于 cakephp 2.*

        谢谢

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-02-13
          • 1970-01-01
          • 1970-01-01
          • 2014-03-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多