来自数据库的Codeigniter动态路由

时间:2019-03-24 13:52:39

标签: php mysql codeigniter

我已经实现了从数据库到Codeigniter的动态路由系统。在动态加载路线时,它向我显示了内部服务器错误500。我已经在其他项目中实现了,但是在这里是不可能的。我正在使用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

控制器

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);
    }

生成的路由

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 个答案:

答案 0 :(得分:1)

您错过了regexp比赛的括号。即:any应该类似于(:any)。如下更新路线,然后尝试。

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