【问题标题】:Codeigniter Setting Homepage ( Default Controller )Codeigniter 设置主页(默认控制器)
【发布时间】:2013-08-13 02:41:11
【问题描述】:

我正在尝试在我的 codeigniter 应用程序中实现页面模板,模板工作正常,例如,我有一个博客页面,我试图将其指定为我的主页,具有不同的视图和分页等等。当我写 www.mywebsite.com/blog 时,它会在主页模板中打开,或者假设我有一个页面 www.mywebsite.com/about ,它也会在页面模板中打开。但是当我尝试通过 www.mywebsite.com 访问我的网站时,我有一个 404 错误页面。如何将我的博客页面指定为主页?

页面控制器:

class Page extends Frontend_Controller {

public function __construct(){
    parent::__construct();
    $this->load->model('page_m');
}

public function index() {

// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());

add_meta_title($this->data['page']->title);
add_meta_keyword($this->data['page']->keywords);
add_meta_description($this->data['page']->description);

// Fetch the page data
$method = '_' . $this->data['page']->template;
if (method_exists($this, $method)) {
        $this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
}

// Load the view
$this->data['subview'] = $this->data['page']->template;
$this->load->view('_main_layout', $this->data);
}

private function _page(){ // methods }
private function _homepage(){ // methods }
}

在我的路线中,我已将默认控制器设置为页面控制器

$route['default_controller'] = "page";

【问题讨论】:

    标签: php codeigniter codeigniter-2 codeigniter-routing


    【解决方案1】:

    问题是访问www.mywebsite.com时没有URI段。您可以尝试将默认值设置为:

    $this->uri->segment(1 , 'blog')
    

    【讨论】:

      【解决方案2】:

      你的代码

      // Fetch the page template
      $this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
      count($this->data['page']) || show_404(current_url());
      

      uri 段代码

      $this->uri->segment(1)
      

      正在寻找第一个 url 段,当您浏览您的网站时,例如 www.yousite.come/blog 它会找到,但是当您执行 www.yoursite.com 时,第一个 uri 段丢失了,所以它会返回 false ,所以我会显示404 页面。

      解决方案: 您可以将第二个参数添加到函数中,例如

      $this->uri->segment(1,'blog');
      

      现在如果第一个 url 段丢失它不会返回 false,它会返回默认的 vlaue 'blog'

      有关此的更多信息,您可以查看codeingitor documentation

      【讨论】:

      • 链接失效了,猜是here
      【解决方案3】:

      application/config/routes.php

      $route['default_controller'] = 'namecontroller';
      

      【讨论】:

      • 他已经定义了$route['default_controller'] = "page";
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多