Welcome Guest, Not a member yet? Register   Sign In
Routing issue
#1

PHP Code:
$route['posts/create'] = 'PostApi/create';
$route['posts/(:any)'] = 'PostApi/view/$1';
$route['posts'] = 'PostApi/index';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE


These are my routes. I am having an issue where i got to www.blog.com/blog/index.php/posts/create and it takes me www.blog.com/blog/index.php/posts which is the page which displays all the posts. I want it to go to the create page. Below is the PostApi controller which loads the views.it seems to be going to index_get method instead of the create_post method. How do i fix this?

PHP Code:
class postApi extends RestController
{

    function __construct()
    {
        // Construct the parent class
        parent::__construct();
        $this->load->model('Post_model');  
        
        

    
}

    public function index_get(){

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

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


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


    }

    public function view_get($slug NULL)
    {

        $data['post'] = $this->Post_model->get_posts($slug);
      

        
if (empty($data['post'])) {

            show_404();
        }

    
        $data
['title'] = $data['post']['title'];

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

    }


    public function create_post()
    {
        $data['title'] = 'create title';

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




    }




Reply
#2

(This post was last modified: 12-27-2022, 03:27 AM by ruslan.)

(12-26-2022, 01:19 PM)acewithacase Wrote:
PHP Code:
$route['posts/create'] = 'PostApi/create';
$route['posts/(:any)'] = 'PostApi/view/$1';
$route['posts'] = 'PostApi/index';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE


These are my routes. I am having an issue where i got to www.blog.com/blog/index.php/posts/create and it takes me www.blog.com/blog/index.php/posts which is the page which displays all the posts. I want it to go to the create page. Below is the PostApi controller which loads the views.it seems to be going to index_get method instead of the create_post method. How do i fix this?


You should change route names
Because 'posts/create' matches both 'posts/create' and 'posts/(:any)'

May be

Code:
$route['post/create'] = 'PostApi/create';
$route['posts/(:any)'] = 'PostApi/view/$1';
Reply
#3

The (:any) is a catch all route and should be the last routes defined!

PHP Code:
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['posts/create'] = 'PostApi/create';
$route['posts'] = 'PostApi/index';

// the */(:any) is a catch all route and should be the last route defined!
$route['posts/(:any)'] = 'PostApi/view/$1';
$route['(:any)'] = 'pages/view/$1'
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

Is it okay to load views in the rest controllers functions?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB