代码设置主页(默认控制器) [英] Codeigniter Setting Homepage ( Default Controller )

查看:157
本文介绍了代码设置主页(默认控制器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的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(){

//获取页面模板
$ 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);

//获取页面数据
$ method ='_'。 $ this-> data ['page'] - >模板;
if(method_exists($ this,$ method)){
$ this-> $ method();
}
else {
log_message('error','Could not load template'。$ method。'in file'。__FILE__。'at line'。__LINE__);
}

//加载视图
$ this-> data ['subview'] = $ this-> data ['page'] - > template;
$ this-> load-> view('_ main_layout',$ this-> data);
}

私人函数_page(){//方法}
私有函数_homepage(){//方法}
}



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

  $ route ['default_controller'] =page; 


解决方案

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

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


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 ?

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";

解决方案

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

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

这篇关于代码设置主页(默认控制器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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