1

I have implemented a dynamic routing system to my Codeigniter from the database. while am loading the routes dynamically it is showing me an internal server error 500. I have already implemented in my other projects also but it's not possible here. I am using Codeigniter https://github.com/chriskacerguis/codeigniter-restserver

// $route['default_controller'] = 'Home';
// $route['person/(:any)'] = 'Home/person_data/$1';
// $route['filmography/(:any)'] = 'Home/filmo_details/$1';
// $route['biography/(:any)'] = 'Home/bio_details/$1';
// $route['article/(:any)'] = 'Home/article_details/$1';
// $route['jukebox/(:any)'] = 'Home/jukebox/_details/$1';
// $route['admin'] = 'Admin';
// $route['login'] = 'Admin/login';
// $route['404_override'] = '';
// $route['translate_uri_dashes'] = TRUE;

$route[ 'default_controller' ]  = 'Home';
$route['admin'] = 'Admin';
$route['login'] = 'Admin/login';
$route[ '404_override' ]        = 'my404';

require_once(BASEPATH .'database/DB'. EXT);
$db =& DB();
$query = $db->get( '_app_routes' );
$result = $query->result();
// echo"<pre>";print_r($result);exit;
foreach( $result as $row )
{
    $route[ $row->slug ]                 = $row->controller;
    $route[ $row->slug.'/:any' ]         = $row->controller;
    $route[ $row->controller ]           = $row->controller;
    $route[ $row->controller.'/:any' ]   = $row->controller;
}
$route['translate_uri_dashes'] = TRUE;



/*
| -------------------------------------------------------------------------
| Sample REST API Routes
| -------------------------------------------------------------------------
*/
$route['api/example/users/(:num)'] = 'api/example/users/id/$1'; // Example 4
$route['api/example/users/(:num)(\.)([a-zA-Z0-9_-]+)(.*)'] = 'api/example/users/id/$1/format/$3$4'; // Example 8

Controller

public function person_details($personid){
        $data = array();
        $data['persondata']=$this->User_model->getPersondata($personid);
        // echo"<pre>";print_r($data);exit;
        $this->load->view('default/person_details_view',$data);
    }

Routes generated

Array
(
    [0] => stdClass Object
        (
            [ID] => 1
            [slug] => pawan-kalyan
            [controller] => Home/person_details/1
            [PAGE_ID] => 1
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

    [1] => stdClass Object
        (
            [ID] => 2
            [slug] => chiranjeevi
            [controller] => Home/person_details/2
            [PAGE_ID] => 2
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

    [2] => stdClass Object
        (
            [ID] => 3
            [slug] => nagababu
            [controller] => Home/person_details/3
            [PAGE_ID] => 3
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

    [3] => stdClass Object
        (
            [ID] => 4
            [slug] => allu-arjun
            [controller] => Home/person_details/4
            [PAGE_ID] => 4
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

    [4] => stdClass Object
        (
            [ID] => 5
            [slug] => ram-charan-tej
            [controller] => Home/person_details/5
            [PAGE_ID] => 5
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

    [5] => stdClass Object
        (
            [ID] => 6
            [slug] => sai-dharam-tej
            [controller] => Home/person_details/6
            [PAGE_ID] => 6
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

    [6] => stdClass Object
        (
            [ID] => 7
            [slug] => varun-tej
            [controller] => Home/person_details/7
            [PAGE_ID] => 7
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

    [7] => stdClass Object
        (
            [ID] => 8
            [slug] => naga-babu-says-i-don't-know-balakrishna
            [controller] => Home/article_details/1
            [PAGE_ID] => 8
            [PAGE_GROUP_ID] => 1
            [STATUS] => 1001
        )

)

1 Answer 1

2

You have missed the bracket for the regexp match. i.e the :any should be like (:any). Update the route like below and try.

foreach( $result as $row )
{
    $route[ $row->slug ]                 = $row->controller;
    $route[ $row->slug.'/(:any)' ]         = $row->controller;
    $route[ $row->controller ]           = $row->controller;
    $route[ $row->controller.'/(:any)' ]   = $row->controller;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Now am getting the routes, but when I use them it's showing 404 and it is not redirecting to the page @Saji
@SairamDuggi can you show 1 or 2 routes created dynamically?
Updated the question, please check
Can you make the controller name lower strtolower($row->controller); and try
no not working, for a project which is in production is working perfectly with the same code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.