gpt4 book ai didi

php - 来自数据库的 Codeigniter 动态路由

转载 作者:行者123 更新时间:2023-11-29 09:47:59 25 4
gpt4 key购买 nike

我已经实现了从数据库到我的 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

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

生成的路线

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
)

)

最佳答案

您错过了 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;
}

关于php - 来自数据库的 Codeigniter 动态路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55324506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com