【问题标题】:unlimited parameters get last one无限参数获取最后一个
【发布时间】:2016-05-09 09:15:12
【问题描述】:

我正在使用 codeigniter。如何获取我的 URL 的最后一部分,例如:

www.example.com/uk-en/category/subcategory

参数可能没有限制,也可能只有一个。在我的控制器主页和方法索引中,我只想要 url 的最后一部分,例如“子类别”

  1. 如果 url 是 www.example.com/uk-en/category 我想要“类别”
  2. 如果 url 是 www.example.com/uk-en 我想要 "uk-en"

编辑

如果 url 是 www.example.com/Home/index/uk-en/category 那么它正在工作
但是我想要没有类名“Home”和方法“index”

喜欢这个 www.example.com/uk-en/category

​​>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
    public function index($params=[]){
      $params=func_get_args();
     $last=end($params);
     echo $last;
    }
}
?>

routes.php

   <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    $route['(:any)'] = 'Home/index';
    $route['default_controller'] = 'Home';
    $route['404_override'] = 'error_404/index';
    $route['translate_uri_dashes'] = FALSE;
    ?>  

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

【问题讨论】:

  • 如果不出意外,你可以使用parse_url
  • PHP - parse current URL的可能重复
  • Regis Portalez 使用解析当前 URL 如何访问类主页和方法索引。

标签: php .htaccess codeigniter codeigniter-3


【解决方案1】:

在你的溃败中使用它

$route['(.*)'] = "Home/index";

这将在您的索引函数中打印控制器中的所有路线

print_r($this->uri->segment_array());

【讨论】:

    【解决方案2】:

    您的路线中的问题是在首位设置通配符占位符。它总是需要排在最后。

    路由将按照它们定义的顺序运行。较高的路线总是优先于较低的路线。

    $route['default_controller'] = 'Home';
    $route['404_override'] = 'error_404/index';
    $route['translate_uri_dashes'] = FALSE;
    
    //some example routes
    $route['specific/route'] = 'controller/method';
    $route['specific/(:num)'] = 'page/show/$1';
    
    //always put your wildcard route at the end of routes.php file
    $route['(:any)'] = 'home/index';
    

    Docs.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 1970-01-01
      • 2019-02-11
      • 2010-12-23
      • 2015-08-27
      相关资源
      最近更新 更多