url CodeIgniter 中基于城市的控制器功能
City based controller function in url CodeIgniter
我正在使用 CodeIgniter 开发一个网站。
我希望我的站点用户从下拉菜单中选择 select 城市,然后根据该城市显示内容。为此,我有两个问题。
1) 如何向 URL 段添加新参数。我检查了 this 。可以创建城市作为控制器吗?但是我应该如何创建当前控制器呢?如果是这样,
2) 问题是我已经在所有控制器上工作了。
指导我应该如何进行。
您可以将 uri 段作为参数传递给您的控制器。
http://YOUR_URL.com/index.php/city/get/zurich
<?php
class City extends CI_Controller {
public function get($city)
{
echo $city;
}
}
http://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods
编辑
只是给你一个想法:
首先从 URL 中删除 index.php:
创建 .htaccess
文件
RewriteEngine on
RewriteCond !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# When CI is in a subfolder use this line instead
#RewriteRule ^(.*)$ /ci/index.php/ [L,QSA]
RewriteRule ^(.*)$ index.php/ [L,QSA]
打开文件 /application/config/config.php
并搜索行
$config['index_page'] = 'index.php';
并将其更改为
$config['index_page'] = '';
打开文件/application/config/routes.php
并将此行添加到其他规则
$route['(:any)/(:any)/(:any)'] = "//";
控制器看起来像这样。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class City extends CI_Controller {
public function index($city)
{
echo $city;
}
}
所以我只是更改了片段的顺序。 2 = Class,3 = 方法,1 = 参数。
试试这个。
-首先告诉我从下拉列表中选择城市后如何发送请求。
-无论如何我会告诉你,首先将 jquery 'change' 事件添加到下拉列表中,并且在更改时你必须获得下拉列表的当前值作为
例如
$('#idOfDropDownlist').on('change',function(){
var value = $(this).val();
//now send a ajax request to controller to get information about city.
$.ajax({
type:'get',
url:"<?php echo base_url(); ?>ControllerName/methodName/"+value,
success:function(response){
console.log(response);
}
});
});
//---- your controller's method for getting this request will be like.
class ControllerName .....{
public function methodName($city = ''){
//--- here you got the city name,so do whatever u want with......
}
}
我正在使用 CodeIgniter 开发一个网站。
我希望我的站点用户从下拉菜单中选择 select 城市,然后根据该城市显示内容。为此,我有两个问题。
1) 如何向 URL 段添加新参数。我检查了 this 。可以创建城市作为控制器吗?但是我应该如何创建当前控制器呢?如果是这样,
2) 问题是我已经在所有控制器上工作了。
指导我应该如何进行。
您可以将 uri 段作为参数传递给您的控制器。
http://YOUR_URL.com/index.php/city/get/zurich
<?php
class City extends CI_Controller {
public function get($city)
{
echo $city;
}
}
http://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods
编辑
只是给你一个想法:
首先从 URL 中删除 index.php:
创建 .htaccess
文件
RewriteEngine on
RewriteCond !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# When CI is in a subfolder use this line instead
#RewriteRule ^(.*)$ /ci/index.php/ [L,QSA]
RewriteRule ^(.*)$ index.php/ [L,QSA]
打开文件 /application/config/config.php
并搜索行
$config['index_page'] = 'index.php';
并将其更改为
$config['index_page'] = '';
打开文件/application/config/routes.php
并将此行添加到其他规则
$route['(:any)/(:any)/(:any)'] = "//";
控制器看起来像这样。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class City extends CI_Controller {
public function index($city)
{
echo $city;
}
}
所以我只是更改了片段的顺序。 2 = Class,3 = 方法,1 = 参数。
试试这个。 -首先告诉我从下拉列表中选择城市后如何发送请求。 -无论如何我会告诉你,首先将 jquery 'change' 事件添加到下拉列表中,并且在更改时你必须获得下拉列表的当前值作为 例如
$('#idOfDropDownlist').on('change',function(){
var value = $(this).val();
//now send a ajax request to controller to get information about city.
$.ajax({
type:'get',
url:"<?php echo base_url(); ?>ControllerName/methodName/"+value,
success:function(response){
console.log(response);
}
});
});
//---- your controller's method for getting this request will be like.
class ControllerName .....{
public function methodName($city = ''){
//--- here you got the city name,so do whatever u want with......
}
}