简体   繁体   English

CodeIgniter:向URL添加参数

[英]CodeIgniter: adding parameters to URL

Started my first CI project and am just wondering how I handle URL parameters? 开始我的第一个CI项目,我只是想知道如何处理URL参数? I have a controller named 'city', and I've modified my mod_rewrite so localhost/codeigniter uses rewrite to localhost/codeigniter/city. 我有一个名为'city'的控制器,我修改了我的mod_rewrite,因此localhost / codeigniter使用重写到localhost / codeigniter / city。 What I want to do is add a city name onto the end of the URL and use get segment to query a table. 我想要做的是在URL的末尾添加一个城市名称,并使用get segment来查询表。

So my example would be localhost/codeigniter/edinburgh. 所以我的例子是localhost / codeigniter / edinburgh。 I would grab the last segment and then create the sql query. 我会抓住最后一段,然后创建SQL查询。 However I think when I put edinburgh into the URL CI thinks I'm looking for a controller called 'edinburgh'. 但是我认为当我将爱丁堡放入URL时,CI认为我正在寻找一个名为“爱丁堡”的控制器。

Do I have to add routing in or something similar? 我是否必须添加路由或类似的东西?

You can indeed use routing to do this. 您确实可以使用路由来执行此操作。

$route[':any'] = "controller/method"; $ route [':any'] =“controller / method”;

This will redirect EVERYTHING after your base url to the specified controller and method inside that controller. 这将在您的基本URL之后将一切重定向到该控制器内的指定控制器和方法。 To get the url segments you can use the URI helper . 要获取url段,您可以使用URI帮助程序

$this->load->helper('url'); $这个 - >负载>帮手( 'URL'); // load the helper first //首先加载帮助器

$city = $this->uri->segment(1); $ city = $ this-> uri-> segment(1);

When accessing http://localhost/codeigniter/edinburgh the $city variable in above example would be edinburgh. 访问http:// localhost / codeigniter / edinburgh时 ,上面示例中的$ city变量将是edinburgh。

Hope that helps! 希望有所帮助!

PS. PS。 You don't need mod_rewrite to specify a default controller. 您不需要mod_rewrite来指定默认控制器。 You can set it in your config.php under Routes. 您可以在Routes下的config.php中进行设置。 Specify city as your default controller and you can get rid of the mod_rewrite. 将city指定为默认控制器,您可以删除mod_rewrite。

Yes you can use a route : 是的你可以使用路线

$route[':any/'] = "myclass/by_city_method";

But why don't you use a module called (for instance) city to get the classical uri scheme? 但是你为什么不使用一个名为(例如)城市的模块来获得经典的uri方案呢?

class city extends Controller { 
   public void index($city=false) {
       if ($city) { } else { }
   }
}

Edit: you can even choose city to be the default controller, in the global config file. 编辑:您甚至可以在全局配置文件中选择city作为默认控制器。

Another method: 另一种方法:

route.php: route.php:

$route['city/(:any)'] = "city/city_lookup/$1";

city.php city.php

<?php 
class City extends Controller {

    function City()
    {
        parent::Controller();
    }

    function city_lookup($id)
    {
        echo "$id";
    }
}
$path = "/codeignter/city/viewcity/Edinburg";

This will cause that City controller is called, method viewcity is executed with parameter that has value 'Edinburg' is passed! 这将导致调用City控制器,使用值为'Edinburg'的参数执行方法viewcity

Here is code for your controller... 这是控制器的代码......

class city extends Controller { 
   public viewcity($city='') {
       echo ($city === '') ? "The city you want to view is {$city}!" : "not defined!";
   }
}

Amen :) 阿门:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM