简体   繁体   English

CakePHP-如何将页面标题设置为商品名称?

[英]CakePHP - How do i set the page title to an item name?

OK, so I'm trying to teach myself the CakePHP framework, and I'm trying to knock up a simple demo app for myself. 好的,所以我试图自学CakePHP框架,并且试图为自己创建一个简单的演示应用程序。

I have the controllers, views and models all set up and working, but I want to do something slightly more than the basic online help shows. 我已经设置好了所有的控制器,视图和模型并可以正常工作,但是我想做的事情比基本的在线帮助显示的要多。

I have a guitars_controller.php file as follows... 我有一个guitars_controller.php文件,如下所示...

<?php
class GuitarsController extends AppController {
    var $name = 'Guitars';
    function index() {
        $this->set('Guitars', $this->Guitar->findAll());
        $this->pageTitle = "All Guitars";
    }
    function view($id = null) {
        $this->Guitar->id = $id;
        $this->set('guitar', $this->Guitar->read());
        // Want to set the title here.
    }
}
?>

The 'Guitar' object contains an attribute called 'Name', and I'd like to be able to set that as the pageTitle for the individual page views. “吉他”对象包含一个名为“名称”的属性,我希望能够将其设置为各个页面视图的pageTitle。

Can anyone point out how I'd do that, please? 有人可以指出我该怎么做吗?

NB : I know that there is general disagreement about where in the application to set this kind of data, but to me, it is data related. 注意 :我知道在应用程序中的何处设置此类数据存在普遍分歧,但对我而言,这与数据有关。

These actions are model agnostic so can be put in your app/app_controller.php file 这些操作与模型无关,因此可以放在您的app / app_controller.php文件中

<?php
class AppController extends Controller {
    function index() {
        $this->set(Inflector::variable($this->name), $this->{$this->modelClass}->findAll());
        $this->pageTitle = 'All '.Inflector::humanize($this->name);
    }
    function view($id = null) {
        $data = $this->{$this->modelClass}->findById($id);
        $this->set(Inflector::variable($this->modelClass), $data);
        $this->pageTitle = $data[$this->modelClass][$this->{$this->modelClass}->displayField];
    }
}
?>

Pointing your browser to /guitars will invoke your guitars controller index action, which doesn't exist so the one in AppController (which GuitarsController inherits from) will be run. 将浏览器指向/ guitars将调用您的Guitars Controller索引动作,该动作不存在,因此将运行AppController中的那个(GuitarsController继承自该动作)。 Same for the view action. 与视图操作相同。 This will also work for your DrumsController, KeyboardsController etc etc. 这也将适用于您的DrumsController,KeyboardsController等。

You can set this in the controller: 您可以在控制器中进行设置:

function view($id = null) {
    $guitar = $this->Guitar->read(null, $id);
    $this->set('guitar', $guitar);
    $this->pageTitle = $guitar['Guitar']['name'];
}

Or in the view: 或在视图中:

<? $this->pageTitle = $guitar['Guitar']['name']; ?>

The value set in the view will override any value that may have already been set in the controller. 在视图中设置的值将覆盖控制器中可能已经设置的任何值。

For security, you must ensure that your layout / view that displays the pageTitle html-encodes this arbitrary data to avoid injection attacks and broken html 为了安全起见,您必须确保显示pageTitle的布局/视图对这些任意数据进行html编码,以避免注入攻击和损坏的html。

<?php echo h( $title_for_layout ); ?>

In response to your own answer about oo paradigm. 回应您对oo范式的回答。 Its like this : 就像这样 :

function view($id) {
    $this->Guitar->id = $id;
    $this->Guitar->read();
    $this->pageTitle = $this->Guitar->data['Guitar']['name'];
    $this->set('data', $this->Guitar->data);
}

By the way, you should check if id is set and valid etc, since this is url user input. 顺便说一句,您应该检查id是否设置和有效等,因为这是url用户输入。

As of CakePHP 1.3, setting page title has been changed. 从CakePHP 1.3开始,设置页面标题已更改。

$this->pageTitle = "Title"; //deprecated

$this->set("title_for_layout",Inflector::humanize($this->name)); // new way of setting title

Note: More about Inflector: http://api13.cakephp.org/class/inflector#method-Inflector 注意:有关Inflector的更多信息: http ://api13.cakephp.org/class/inflector#method-Inflector

$this->pageTitle = $this->Guitar->Name;

它应该放在View中,但我不是PHP或cakePHP,但这就是视图应该执行的操作,而不是控制器。

It should go in the controller. 它应该放在控制器中。 See this 看到这个

"but I want to do something slightly more than the basic online help shows." “但是我想做的事情比基本的在线帮助显示的要多。”

Isn't that always the rub? 难道不总是这样吗? So much documentation is geared towards a bare minimum that it really does not help much. 如此多的文档旨在达到最低限度,实际上并没有太大帮助。 You can complete many of the tutorials available but as soon as you take 1 step off the reservation the confusion sets in. Well, it's either bare minimum or pro developer maximum but rarely hits that sweet spot of ease, clarity and depth. 您可以完成许多可用的教程,但是一旦您从混乱中撤离了保留,就可以轻松完成第一步。好吧,这是最低限度的要求,还是专业开发人员的最高要求,但很少能达到轻松,清晰和深度的最佳效果。

I'm currently rewriting some Zend Framework documentation for my own use simply so I can smooth out the inconsistencies, clarify glossed over assumptions and get at the core, "best practice" way of understanding it. 我目前正在简单地重写一些Zend Framework文档供我自己使用,这样我就可以消除不一致之处,弄清假设的局限性,并获得理解它的核心“最佳实践”方式。 My mantra: Ease, clarity, depth. 我的口头禅:轻松,清晰,深入。 Ease, clarity, depth. 轻松,清晰,深度。

Ah, the none-obvious answer is as follows... 嗯,答案不是很明显如下...

$this->pageTitle = $this->viewVars['guitar']['Guitar']['Name'];

I found this by placing the following code in the controller (was a long shot that paid off, to be honest) 我通过在控制器中放置以下代码发现了这一点(老实说,这是一个成功的回报)

echo "<pre>"; print_r($this);echo "</pre>";

Thanks to all those that tried to help. 感谢所有尝试提供帮助的人。

OK, I really want to set the page title in the controller instead in the view. 好的,我真的很想在控制器中而不是在视图中设置页面标题。 So here's what I did: 所以这就是我所做的:

class CustomersController extends AppController {

    var $name = 'Customers';

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('menu',$this->name);
        switch ($this->action) {
          case 'index':
            $this->title = 'List Customer';
            break;
          case 'view':
            $this->title = 'View Customer';
            break;
          case 'edit':
            $this->title = 'Edit Customer';
            break;
          case 'add':
            $this->title = 'Add New Customer';
            break;
          default:
            $title = 'Welcome to '.$name;
            break;
        }
        $this->set('title',$this->title);
      }

The trick is that you can't set $this->title inside any action, it won't work. 诀窍是您不能在任何动作中设置$this->title ,它将无法工作。 It seems to me that the web page reaches action after rendering, however you can do it in beforeFilter . 在我看来,网页在渲染后即会执行操作,但是您可以在beforeFilter

echo "<pre>"; print_r($this);echo "</pre>";

怎么样

pr( $this );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM