简体   繁体   English

Codeigniter 使用额外的 url 参数

[英]Codeigniter use extra url parameter

In codeigniter I want to have one url segment to not be consider.在 codeigniter 中,我希望不考虑一个 url 段。

example.com/app/dev1/controller/function/id
example.com/app/dev2/controller/function/id

base url is基本网址是

baseUrl = example.com/app

When I use this type of url, codeigniter consider "dev1" as controller, "controller" as function, so I get error of page not found.当我使用这种类型的 url 时,codeigniter 将“dev1”视为控制器,将“controller”视为函数,因此出现找不到页面的错误。

I want to know if I can code which don't consider first parameter as controller and it start considering from second parameters.我想知道我是否可以编写不将第一个参数视为控制器的代码,而是从第二个参数开始考虑。

I can not add in base url as it is not constant we can have en, fr, nl etc so do we have anything that help in this case I don't want to add query string "?".我无法添加基本 url,因为它不是常量,我们可以使用 en、fr、nl 等,所以在这种情况下我们有什么帮助吗?我不想添加查询字符串“?”。 Can we do anything using .htaccess我们可以使用 .htaccess 做任何事情吗

suppose url is like following : www.paintes.com/painters-in-chennai then route can be like following假设 url 如下所示:www.paintes.com/painters-in-chennai 然后路由可以如下所示

    $route['painters-in-(:any)'] ='index/paintersIn/$1';

in index.php controller, i'll be able to receive chennai like followingindex.php控制器中,我将能够像下面这样接收 chennai

    function paintesIn($city_name)
    {
        echo $city_name //OUTPUT WILL BE "chennai"
    }

if url is like www.paintes.com/painters-in-mumbai , then output will be mumbai如果 url 类似于 www.paintes.com/painters-in-mumbai ,则输出将是 mumbai

as per your request, URL is liek following : www.paintes.com/mumbai-painters then you can write like following根据您的要求,网址如下:www.paintes.com/mumbai-painters 然后您可以像下面这样写

    $route['(:any)-painters'] ='index/paintersIn/$1';

You have to configure your CodeIgniter routes properly.您必须正确配置 CodeIgniter 路由。 Example:例子:

$route['dev1/controller/fun/(:num)']  = "dev1/controller/fun/index/$1";

See https://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/https://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/

You could add a URI Routing regular expression like this to skip the dev part and go straight to the desired controller :您可以添加这样的 URI 路由正则表达式以跳过dev部分并直接转到所需的控制器:

$route['dev(:num)/([a-z]+)/([a-z]+)/(\d+)'] = '$2/$3/$4'; // rule to match method with parameter
$route['dev(:num)/([a-z]+)/([a-z]+)'] = '$2/$3'; // rule to match method without parameter

Or if you don have a common dev URI segment, you could use :any rule :或者,如果您没有通用的dev URI 段,则可以使用:any规则:

$route['(:any)/([a-z]+)/([a-z]+)/(\d+)'] = '$2/$3/$4'; // rule to match method with parameter
$route['(:any)/([a-z]+)/([a-z]+)'] = '$2/$3'; // rule to match method without parameter

You might want to go for the following in routing您可能希望在路由中执行以下操作

$route['dev1/(:any)'] ='dev1/$1';
$route['dev1/(:any)/(:any)'] ='dev1/$1/$2';
$route['dev1/(:any)/(:any)/(:any)'] ='dev1/$1/$2/$3';

Its rare that Codeigniter treats folder name as controller, try renaming folderas well.其罕见的, Codeigniter对待文件夹名称作为控制器,尝试重命名folderas好。

Otherwise above solution should work.否则上述解决方案应该有效。

we will neeed to create new file "MY_Router.php" in "/application/core" folder, with following content我们需要在“/application/core”文件夹中创建新文件“MY_Router.php”,内容如下

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

class MY_Router extends CI_Router {

 protected function _parse_routes()
 {
    // do logic you needed

    unset($this->uri->segments[1]);

    // Return default function
    return parent::_parse_routes();
 }  
}

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

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