我有一个codeignte项目我旧版本我升级我的项目在codeigite 3.1.11和PHP 7.1一切都很好,但我的自定义路由不工作。但我以前的项目这条路线运行良好。请检查我的代码并给我一些解决方案。我正在尝试改变我的访问方式和路线。如果使用config uri_protocol是REQUEST_URI
这里是我的roup .php
$url = $_SERVER['REQUEST_URI'];
$url = explode('/',$url);
$url=end($url);
$secondlasturl = explode('/',$_SERVER['REQUEST_URI']);
$controller = $secondlasturl[1];
$second_url = $secondlasturl[2];
if(isset($secondlasturl[3])){
$third_url = $secondlasturl[3];
}else{
$third_url='';
}
//$min=explode('-',$second_url1);
$con = mysqli_connect('localhost','root','','mydb');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
if($url!=''){
$sql = 'select CompanyID, shorturl, BusinessType from lxn_companies where shorturl ="'.$url.'"';
$res=mysqli_query($con, $sql);
if(mysqli_fetch_assoc($res))
{
$row = mysqli_fetch_row($res);
//print_r($row);
$type = $row[2];
if($type=='Exporters')
{
$userlink = 'ExporterDetail/'.$row[0];
}
else
{
$userlink = 'ImporterDetail/'.$row[0];
}
$route['(:any)'] = "user/".$userlink."";
}
else
{
$sql = 'select CompanyID, sef_url, BusinessType from lxn_companies where sef_url ="'.$url.'"';
$res=mysqli_query($con, $sql);
if(mysqli_num_rows($res)>0)
{
$row = mysqli_fetch_row($res);
//print_r($row);
$type = $row[2];
if($type=='Exporters')
{
$userlink = 'ExporterDetail/'.$row[0];
}
else
{
$userlink = 'ImporterDetail/'.$row[0];
}
$route['(.*)'] = "user/".$userlink."";
}
}
}
我正在使用这个haccess代码为我的项目。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-z0-9_-]+).html$ index.php/page/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|asset|robots.txt)
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php71” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php71 .php .php7 .phtml
</IfModule>
根据您现有的代码,我想出了这个供您尝试。
是的,它正在使用mysqli而不是PDO(还没有),所以其他人,请不要为此火化我。
设置:(如果需要,你需要在以后的问题中提供这类信息。)
DROP TABLE IF EXISTS lxn_companies;
CREATE TABLE lxn_companies (
CompanyID INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
BusinessType VARCHAR(48),
shorturl VARCHAR(128),
sef_url VARCHAR(128)
);
INSERT INTO lxn_companies
(CompanyID, BusinessType, shorturl, sef_url) values
(1,'Exporters','CompanyID1_short_url',''),
(2,'Importers','CompanyID2_short_url',''),
(3,'Exporters','','CompanyID3_sef_url'),
(4,'Importers','','CompanyID4_sef_url');
"roue.php"文件
当你看到代码重复时(就像你的代码一样),它就需要被重构。
我现在没有时间解释这里的每一件事,但是你可以读一遍。
要点-使用mysqli_fetch_assoc(关联)读取DB行列(字段),所以使用名称(更可读),而不是数字索引(难以理解)。
$url_array = explode('/', $_SERVER['REQUEST_URI']);
$url = end($url_array);
// The Following 3 variables are not used in this code.
//$controller = isset($url_array[1]) ? $url_array[1] : '';
//$second_url = isset($url_array[2]) ? $url_array[2] : '';
//$third_url = isset($url_array[3]) ? $url_array[3] : '';
if ($url != '') {
$con = mysqli_connect('localhost', 'root', '', 'mydb');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Look for entries with either shorturl or sef_url.
// A Row cannot have both shorturl and sef_url
$sql = "SELECT
CompanyID,
BusinessType
FROM lxn_companies
WHERE shorturl = '{$url}'
OR sef_url = '{$url}'
";
$res = mysqli_query($con, $sql);
if ($res && mysqli_num_rows($res)) {
$row = mysqli_fetch_assoc($res);
if ($row['BusinessType'] == 'Exporters') {
$userlink = 'ExporterDetail/';
} else {
$userlink = 'ImporterDetail/';
}
$route['(.*)'] = "user/" . $userlink . $row['CompanyID'];
}
}
// Debug Stuff
echo '<pre>';
echo 'LINE: ' . __LINE__ . ' Module ' . __CLASS__ . '<br>';
var_dump($route);
echo '</pre>';
exit('Stop here to inspect the final routes');
我"included"该文件位于应用程序中的/config/routes.php.
需要修复的地方:用PDO替换mysqli