无限参数获取最后一个 [英] unlimited parameters get last one

查看:136
本文介绍了无限参数获取最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用codeigniter。如何获取网址的最后一部分,例如:


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


参数可以是无限的,也可以只有一个。在我的Controller Home和方法Index的里面,我只想要url的最后一部分,例如subcategory


  1. 如果url是www.example.com / uk-en / category我想要category

  2. 如果url是www.example.com/uk-en我想要uk-en / ol>




    编辑



    如果url是www.example.com/首页/ index / uk-en / category然后它工作

    但是我想要没有类名Home和方法index



    Like this 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('不允许直接脚本访问);
    $ route ['(:any)'] ='Home / index';
    $ route ['default_controller'] ='家';
    $ route ['404_override'] ='error_404 / index';
    $ route ['translate_uri_dashes'] = FALSE;
    ?>

    .htaccess

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


    解决方案

    在您的路径中使用

      $ route ['(。*)'] =首页/索引; 

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

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


    I'm using codeigniter. How can I get last part of my URL like:

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

    Parameters may be unlimited or there may be only one. Inside of my Controller Home and method Index, I just want last part of url e.g "subcategory"

    1. if url is www.example.com/uk-en/category I want "category"
    2. if url is www.example.com/uk-en I want "uk-en"


    Edit

    if url is www.example.com/Home/index/uk-en/category then it's working
    but What i want without class name "Home" and method "index"

    Like this 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]
    

    解决方案

    Use this in your routs

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

    This to print all routs in your controller in your index function

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

    这篇关于无限参数获取最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆