如何从codeigniter中的url获取参数?
我无法从codeigniter中的url获取参数值。 例如:如何从codeigniter中的url获取参数?,codeigniter,Codeigniter,我无法从codeigniter中的url获取参数值。 例如:localhost/log/job/php这里log/是我的文件夹,job/是我的控制器,php是我的参数 我想在控制器“作业”中获取此参数。 我该怎么做呢?您可以使用$this->uri->segment(n) 假设您的参数最终总是: $segs = $this->uri->segment_array(); echo end($segs); 编辑:用于澄清其他要点。首先,您需要设置应用程序/config/routes.p
localhost/log/job/php
这里log/
是我的文件夹,job/
是我的控制器,php
是我的参数
我想在控制器“作业”中获取此参数。
我该怎么做呢?您可以使用
$this->uri->segment(n)
假设您的参数最终总是:
$segs = $this->uri->segment_array();
echo end($segs);
编辑:用于澄清其他要点。首先,您需要设置应用程序/config/routes.php
:
$route['Youruri-(:any)/(:num)'] = "yourcontroller/yourfunction/$1/$2";
$route['Youruri-(:any)'] = "yourcontroller/yourfunction/$1";
在控制器应用程序/controllers/yourcontroller.php
中,您需要定义一个函数:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Yourcontroller extends CI_Controller {
function yourfunction($brand = null, $page = null) {
// http://www.yoursite.com/Youruri-Microsoft
// or http://www.yoursite.com/yourcontroller/yourfunction/Microsoft
// do some stuff for get products of this $brand (Microsoft)
if($page != null) {
// http://www.yoursite.com/Youruri-Intel/2
// or http://www.yoursite.com/yourcontroller/yourfunction/Intel/2
// do some other stuff get products of this $brand (Intel) of $page (2)
}
}
}
您可以通过以下方式获得:
$this->uri->segment(n);
其中,n=1表示控制器,n=2表示方法,n=3表示参数,依此类推
需要n=3才能获得参数
在路径localhost/log/job/php
中,缺少方法名
即使您的方法名是index
,您的路由也将是localhost/log/job/index/php
如果您需要从url中删除index.php,那么您将使用localhost/log/index.php/job/index/php
要删除index.php,需要通过以下步骤创建.htaccess文件:
创建一个.htaccess文件,其中index.php文件包含内容
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
确保apache可以访问此.htaccess文件。要执行此操作,请编辑apache配置文件。如果您使用ubuntu,那么它是/etc/apache2/sites available/default
,然后将目录和www目录的AllowOverride none
更改为AllowOverride all
<Directory />
Options FollowSymLinks
AllowOverride all
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
最后,不要忘记重新启动apache
希望这会有所帮助。您可以使用$this->uri->segment(n)代码>
您需要对路由进行一些更改,以便允许代码点火器接收控制器中的参数
$route['uri-(:any)/(:num)'] = "controller/function/$1/$2";
或
$route['uri-(:any)'] = "controller/function/$1";
在控制器中进行一些更改
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class controller extends CI_Controller
{
function function($parameter1 = null, $parameter2 = null)
{
........
}
}
使用:
在文件夹中添加.htaccess(在您的案例中是“log”
):
非常感谢。如果我这样做,显示页面未找到错误,您可能需要配置您的路由。@wolfgang实际上我不知道如何配置我的路由,您能帮助我吗您可能需要配置您的路由。我尝试过只显示控制器名称,但不显示我的路由parameter@M.Athishkrishna带有段的代码始终显示url的最后一部分。我认为您没有通过uri向应用程序传递参数。
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class controller extends CI_Controller
{
function function($parameter1 = null, $parameter2 = null)
{
........
}
}
$param = $this->uri->segment(3);
RewriteEngine On
RewriteBase /log/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /log/index.php/$1 [L]