CodeIgniter Forums
Pagination , Routing 404 - Newbie - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Pagination , Routing 404 - Newbie (/showthread.php?tid=67944)



Pagination , Routing 404 - Newbie - anoopd - 04-28-2017

Learning CI3  ..... building a simple cms  . Added ' pagination' to the libraries array and tried the code below 

In the posts controller index function i am showing all my posts ( Total of 7 posts of which i try to show 5)  . When i try to use pagination it is showing 404 error 

 ie;  http://localhost/CI3/posts  - shows all 7 posts with pagination links in bottom , clicking '2' takes to http://localhost/CI3/posts/5  which is giving a 404 page not found . 


route.php
------------
PHP Code:
$route['posts/create'] = 'posts/create';
$route['posts/update'] = 'posts/update';
$route['posts/success'] = 'posts/success';
$route['posts'] = 'posts/index';
$route['posts/(:any)'] = 'posts/view/$1';


$route['default_controller'] = 'pages/view';

$route['categories'] = 'categories/index';
$route['categories/create'] = 'categories/create';
$route['categories/posts/(:any)'] = 'categories/posts/$1';


$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE

Posts.php
------------

PHP Code:
Class Posts extends CI_Controller {

    public function index() {

        $data['title'] = 'Latest Posts';

        $config['base_url'] = site_url().'/posts';
        $config['total_rows'] = $this->post_model->count();
        $config['per_page'] = 5;
        //$config['page_query_string'] = TRUE;
        $config['uri_segment'] = 4;

        $this->pagination->initialize($config);

        $data['pagination_links'] = $this->pagination->create_links();

        $data['posts'] = $this->post_model->get_posts();

        $this->load->view('templates/header');
        $this->load->view('posts/index',$data);
        $this->load->view('templates/footer');

    

Can somebody help with what went wrong  ..


RE: Pagination , Routing 404 - Newbie - arma7x - 04-29-2017

$route ['posts/(:any)' ] = 'posts/view/$1' ;
This route find single post. In your pagination, '/post/{page}' will find post using page param not navigate to other pages and I'm sure in your Post controller searching for invalid post will show 404 page. Btw at the bottom of route, $route ['(:any)' ] = 'pages/view/$1' ; also for viewing single post.


RE: Pagination , Routing 404 - Newbie - InsiteFX - 04-29-2017

Also the default controller cannot be within a sub-directory.


RE: Pagination , Routing 404 - Newbie - anoopd - 04-29-2017

(04-29-2017, 02:17 AM)arma7x Wrote: $route ['posts/(:any)' ] = 'posts/view/$1' ;
This route find single post. In your pagination, '/post/{page}' will find post using page param not navigate to other pages and I'm sure in your Post controller searching for invalid post will show 404 page. Btw at the bottom of route, $route ['(:any)' ] = 'pages/view/$1' ; also for viewing single post.


Thank you for the reply , but as a newbie i just tried removing that route , but still the pagination is not working for me . Can you please let me know how to make the pagination work ?


RE: Pagination , Routing 404 - Newbie - anoopd - 04-29-2017

(04-29-2017, 04:25 AM)InsiteFX Wrote: Also the default controller cannot be within a sub-directory.

It should be the 'index' method in the controller right  ?


RE: Pagination , Routing 404 - Newbie - InsiteFX - 04-30-2017

No it would be your default page shown when someone visits your site like home, welcome or splash page.


RE: Pagination , Routing 404 - Newbie - anoopd - 04-30-2017

(04-30-2017, 03:58 AM)InsiteFX Wrote: No it would be your default page shown when someone visits your site like home, welcome or splash page.

Oh got it , thank you  ...... i am still stuck with pagination , can you please guide me on that  .....