Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改页面标题-确定当前页面的内容_Php_Codeigniter - Fatal编程技术网

Php 更改页面标题-确定当前页面的内容

Php 更改页面标题-确定当前页面的内容,php,codeigniter,Php,Codeigniter,我正在研究codeigniter,我想知道动态更改标题的最佳方法是什么。如果你在主页、单篇文章页面、分类页面等上,标题会发生变化 我能想到的唯一解决方案是创建单独的函数,并将当前URL(来自地址栏)和单个帖子页面、分类页面、主页的结构进行比较 大概是这样的: public function current_title() { if($this->uri->segment(2) == 'post') { // will return post title

我正在研究codeigniter,我想知道动态更改标题的最佳方法是什么。如果你在主页、单篇文章页面、分类页面等上,标题会发生变化

我能想到的唯一解决方案是创建单独的函数,并将当前URL(来自地址栏)和单个帖子页面、分类页面、主页的结构进行比较

大概是这样的:

public function current_title() { 
   if($this->uri->segment(2) == 'post') { 
      // will return post title 
    }

   if($this->uri->segment(2) == 'category') { 
      // will return archive title 
    }

    if(current_url() == base_url()) { 
      // this is home page 
    }
如果以前有人用过这个,任何建议都非常感谢

我就是这样做的:

$PHPFile = basename($_SERVER['PHP_SELF'],'.php');
switch ($PHPFile) {
    case 'index': $PageTitle = 'Home'; break;
    case 'products': $PageTitle = 'Products'; break;
    case 'services': $PageTitle = 'Services'; break;
}
您可以使用字符串搜索或任何需要的内容。我使用此方法是因为我在库中将页眉作为函数。

我就是这样做的:

$PHPFile = basename($_SERVER['PHP_SELF'],'.php');
switch ($PHPFile) {
    case 'index': $PageTitle = 'Home'; break;
    case 'products': $PageTitle = 'Products'; break;
    case 'services': $PageTitle = 'Services'; break;
}

您可以使用字符串搜索或任何需要的内容。我之所以使用此方法,是因为我将页面标题作为库中的函数。

我不会为此使用uri,而是使用控制器和操作名称以及语言类:

public function current_title() 
{
    $this->lang->load('titles.php', 'en');

    return $this->lang->line(
        $this->router->fetch_class().'.'.$this->router->fetch_method()
    );
}
您将有一个类似于
MyClass.myMethod
的键用于翻译。只需在
titles.php
文件中添加标题:

$lang['MyClass.myMethod'] = "The title";
$lang['MyOtherClass.myOtherMethod'] = "The other title";
阅读有关翻译的更多信息:


我不会为此使用uri,而是使用控制器和操作名称以及语言类:

public function current_title() 
{
    $this->lang->load('titles.php', 'en');

    return $this->lang->line(
        $this->router->fetch_class().'.'.$this->router->fetch_method()
    );
}
您将有一个类似于
MyClass.myMethod
的键用于翻译。只需在
titles.php
文件中添加标题:

$lang['MyClass.myMethod'] = "The title";
$lang['MyOtherClass.myOtherMethod'] = "The other title";
阅读有关翻译的更多信息:


//在控制器中,您应该执行以下操作:

 class Home extends your_Controller {
      public function __construct() {
          parent:: __construct();
         }
       function index()
       { 
       $this->data['pageTitle'] = 'Your page title';
        $data['main_content'] = 'home';
        $this->load->view('includefolder/viewname', $data);
       }
    }

//在控制器中,您应该执行以下操作:

 class Home extends your_Controller {
      public function __construct() {
          parent:: __construct();
         }
       function index()
       { 
       $this->data['pageTitle'] = 'Your page title';
        $data['main_content'] = 'home';
        $this->load->view('includefolder/viewname', $data);
       }
    }

因为每个视图都有一个控制器函数,所以您可以很容易地从url获取函数名

$this -> router -> fetch_module();

因此,您可以使用它。

因为每个视图都有一个控制器函数,所以您可以轻松地从url获取函数名

$this -> router -> fetch_module();

因此,您可以使用它。

只需将标题从控制器传递到view@Charles0429确切地谢谢:)哈哈,太简单了,我不知道怎么想不出来。无缘无故地复杂化:|只需将标题从控制器传递给view@Charles0429确切地谢谢:)哈哈,太简单了,我不知道怎么想不出来。无缘无故地复杂化:|我用同样的方式,它是干净和逻辑的。我用同样的方式,它是干净和逻辑的。