CakePHP - 如何将页面标题设置为项目名称? [英] CakePHP - How do i set the page title to an item name?

查看:164
本文介绍了CakePHP - 如何将页面标题设置为项目名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我想教自己CakePHP框架,我想为自己打开一个简单的演示应用程序。

OK, so I'm trying to teach myself the CakePHP framework, and I'm trying to knock up a simple demo app for myself.

我有控制器,视图和模型都设置和工作,但我想做一些稍微超过基本的在线帮助显示。

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.

我有一个guitars_controller.php文件如下...

I have a guitars_controller.php file as follows...

<?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.
    }
}
?>

Guitar对象包含一个名为Name的属性,我希望能够将其设置为单个页面视图的pageTitle。

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.

任何人都可以指出我该怎么做?

Can anyone point out how I'd do that, please?

NB :我知道应用程式在哪里设定这类资料有一般分歧,但对我来说,

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.

推荐答案

这些操作是模型不可知的,因此可以放在app / app_controller.php文件中

These actions are model agnostic so can be put in your app/app_controller.php file

<?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];
    }
}
?>

将浏览器指向/ guitars会调用您的吉他控制器索引操作,一个在AppController(其中GuitarsController继承自)将运行。同样的视图动作。这也将适用于您的DrumsController,KeyboardsController等等。

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. Same for the view action. This will also work for your DrumsController, KeyboardsController etc etc.

这篇关于CakePHP - 如何将页面标题设置为项目名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆