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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18175934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:06:09  来源:igfitidea点击:

Codeigniter Setting Homepage ( Default Controller )

phpcodeignitercodeigniter-2codeigniter-routing

提问by Serip88

I'm trying to implement page templating in my codeigniter application, templating is working fine for instance, i have a blog page, which i'm trying to assign as my homepage, with different view and pagination and so on. When i write www.mywebsite.com/blog, it gets opened in homepage template, or assume that i have a page www.mywebsite.com/about , it gets opened in page template too. But when i try to access my website via www.mywebsite.com, i have a 404 error page. How can i assign my blog page as the homepage ?

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

Page Controller :

页面控制器:

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

in my Routes, i have set my default controller to page controller

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

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

采纳答案by koala_dev

The problem is that there's no URI segment when you visit www.mywebsite.com. You can try setting the default value as:

问题是当您访问www.mywebsite.com. 您可以尝试将默认值设置为:

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

回答by Serip88

application/config/routes.php

应用程序/配置/routes.php

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

回答by Dexture

Your code

你的代码

// 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());

the uri segment code

uri 段代码

$this->uri->segment(1)

is looking first url segment, when you browse your site like www.yousite.come/blog it will work find but when you do www.yoursite.com 1st uri segment is missing so it will return false , so i will show 404 page.

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

Solution :You can just add the second parameter to the function like

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

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

now if the first url segment is missing it will not return false,it will return the default vlaue 'blog'

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

For more information about this you can see codeingitor documentation

有关这方面的更多信息,您可以查看codeingitor 文档