Codeigniter Setting Homepage ( Default Controller )
Asked Answered
Q

3

8

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";
Quadrillion answered 11/8, 2013 at 19:37 Comment(0)
V
0

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')
Villar answered 11/8, 2013 at 19:50 Comment(0)
M
10

application/config/routes.php

$route['default_controller'] = 'namecontroller';
Minify answered 28/10, 2015 at 4:2 Comment(1)
he already define that $route['default_controller'] = "page";Gefen
V
0

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')
Villar answered 11/8, 2013 at 19:50 Comment(0)
J
0

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

$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.

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'

For more information about this you can see codeingitor documentation

Jayejaylene answered 13/11, 2013 at 9:46 Comment(1)
Link is dead now, guess it's herePartial

© 2022 - 2024 — McMap. All rights reserved.