Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/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 Codeigniter设置主页(默认控制器)_Php_Codeigniter_Codeigniter 2_Codeigniter Routing - Fatal编程技术网

Php Codeigniter设置主页(默认控制器)

Php Codeigniter设置主页(默认控制器),php,codeigniter,codeigniter-2,codeigniter-routing,Php,Codeigniter,Codeigniter 2,Codeigniter Routing,我正在尝试在我的codeigniter应用程序中实现页面模板,模板工作正常。例如,我有一个博客页面,我试图将其指定为我的主页,具有不同的视图和分页等。当我写www.mywebsite.com/blog时,它会在主页模板中打开,或者假设我有一个页面www.mywebsite.com/about,它也会在页面模板中打开。但是当我试图通过www.mywebsite.com访问我的网站时,我有一个404错误页面。如何将我的博客页面指定为主页 页面控制器: class Page extends Front

我正在尝试在我的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";

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

$this->uri->segment(1 , 'blog')
你的代码

// 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,它将返回默认的vlue“blog”


有关这方面的更多信息,请参见

application/config/routes.php

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

他已经定义了
$route['default\u controller']=“page”链接现在死了,我猜是