gpt4 book ai didi

php - CodeIgniter - 动态生成路由

转载 作者:可可西里 更新时间:2023-11-01 00:07:01 24 4
gpt4 key购买 nike

我有一个带有动态导航菜单的网站。我将 Controller (葡萄牙语)名称与英语翻译一起保存在数据库中。

我想知道是否有可能在运行时影响“路由”数组,因此它会创建这些路由并在页面加载时缓存它。

希望我说得够清楚,谢谢你的帮助

最佳答案

你可以这样做:

创建一个名为 Routes 的表

--
-- Table structure for table `Routes`
--

CREATE TABLE IF NOT EXISTS `Routes` (
`idRoutes` int(11) NOT NULL AUTO_INCREMENT,
`Order` int(11) NOT NULL,
`Url` varchar(250) NOT NULL,
`Url_Variable` varchar(20) NOT NULL,
`Class` text NOT NULL,
`Method` text NOT NULL,
`Variable` text NOT NULL,
`Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`idRoutes`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=67 ;

在config目录下创建一个名为pdo_db_connect.php的文件

把它放进去并相应地改变。

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function pdo_connect(){

try{

// Include database info
include 'database.php';

if(!isset($db)){
echo 'Database connection not available!';
exit;
}
$dbdriver = $db['default']['dbdriver'];//'mysql';
$hostname = $db['default']['hostname'];//'localhost';
$database = $db['default']['database'];//'config';
$username = $db['default']['username'];//'root';
$password = $db['default']['password'];//'password';

//to connect
$DB = new PDO($dbdriver.':host='.$hostname.'; dbname='.$database, $username, $password);
return $DB;

}catch(PDOException $e) {
echo 'Please contact Admin: '.$e->getMessage();
}

}

现在在你的路由文件中你可以这样做:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Include our PDO Connection
include('application/config/pdo_db_connect.php');

class dynamic_route{

public $pdo_db = FALSE;

public function __construct(){

}
private function query_routes(){
try{

$routes_query = $this->pdo_db->query('SELECT * FROM Routes ORDER BY `Order` ASC');

if($routes_query){
$return_data = array();
foreach($routes_query as $row) {
$return_data[] = $row;
}
return $return_data;

}

}catch(PDOException $e) {
echo 'Please contact Admin: '.$e->getMessage();
}

}
private function filter_route_data($data){

$r_data = array();
foreach($data as $row){
$return_data = new stdClass;

if(empty($row['Url_Variable']) ){
$return_data->url = $row['Url'];
}else{
$return_data->url = $row['Url'].'/'.$row['Url_Variable'];
}

if(empty($row['Method']) && empty($row['Variable']) ){
$return_data->route = $row['Class'];

}elseif(!empty($row['Method']) && empty($row['Variable']) ){
$return_data->route = $row['Class'].'/'.$row['Method'];
}elseif(!empty($row['Method']) && !empty($row['Variable']) ){
$return_data->route = $row['Class'].'/'.$row['Method'].'/'.$row['Variable'];
}

$r_data[] = $return_data;
}
return $r_data;
}
public function get_routes(){
$route_data = $this->query_routes();
$return_data = $this->filter_route_data($route_data);
return $return_data;
}

}

$dynamic_route = new dynamic_route;
// Give dynamic route database connection
$dynamic_route->pdo_db = pdo_connect();
// Get the route data
$route_data = $dynamic_route->get_routes();
//Iterate over the routes
foreach($route_data as $row){
$route[$row->url] = $row->route;
}

关于php - CodeIgniter - 动态生成路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5826282/

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