【问题标题】:How to get the parameter from url in codeigniter?如何从codeigniter中的url获取参数?
【发布时间】:2015-04-18 14:16:50
【问题描述】:

我无法从 codeigniter 中的 url 获取参数值。 例如:localhost/log/job/php 这里log/ 是我的文件夹,job/ 是我的控制器,php 是我的参数。

我想在控制器“作业”中获取此参数。 我该怎么做?

【问题讨论】:

  • 您可能需要配置您的路线。

标签: codeigniter


【解决方案1】:

你可以使用$this->uri->segment(n);

您需要对路由进行一些更改,以允许代码点火器在您的控制器中接收参数

$route['uri-(:any)/(:num)'] = "controller/function/$1/$2";

$route['uri-(:any)'] = "controller/function/$1";

在你的控制器中做一些改变

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

 class controller extends CI_Controller 
 {
    function function($parameter1 = null, $parameter2 = null) 
    {
       ........
    }
 }

参考这个http://www.codeigniter.com/userguide2/libraries/uri.html

【讨论】:

    【解决方案2】:

    您可以使用$this-&gt;uri-&gt;segment(n)

    http://www.codeigniter.com/userguide2/libraries/uri.html

    【讨论】:

    • 谢谢,如果我这样做会显示页面未找到错误
    • 您可能需要配置您的路线。
    • @wolfgang 实际上我不知道如何配置我的路线你能帮我吗
    【解决方案3】:

    假设你的参数总是在最后:

    $segs = $this->uri->segment_array();
    echo end($segs);
    

    编辑:为了澄清其他要点。首先你需要设置你的application/config/routes.php

    $route['Youruri-(:any)/(:num)'] = "yourcontroller/yourfunction/$1/$2";
    $route['Youruri-(:any)'] = "yourcontroller/yourfunction/$1";
    

    在控制器application/controllers/yourcontroller.php中需要定义一个函数:

    <?php
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    
    class Yourcontroller extends CI_Controller {
    
    function yourfunction($brand = null, $page = null) {
       // http://www.yoursite.com/Youruri-Microsoft
       // or http://www.yoursite.com/yourcontroller/yourfunction/Microsoft
       // do some stuff for get products of this $brand (Microsoft)
       if($page != null) {
       // http://www.yoursite.com/Youruri-Intel/2 
       // or http://www.yoursite.com/yourcontroller/yourfunction/Intel/2
       // do some other stuff get products of this $brand (Intel) of $page (2)
       }
    }
    }
    

    【讨论】:

    • 我试过只显示控制器名称但不显示我的参数
    • @M.Athishkrishna 带有分段的代码始终显示 url 的最后一部分。我认为您没有通过 uri 将参数传递给您的应用程序。
    【解决方案4】:

    你得到它:

    $this->uri->segment(n); 
    

    其中 n = 1 表示控制器,n = 2 表示方法,n = 3 表示参数等等。

    你需要 n = 3 来获取参数。

    在您的路径 localhost/log/job/php 中,您的方法名称丢失。

    即使你的方法名是index,那么你的路由也会是localhost/log/job/index/php

    如果您需要从 url 中删除 index.php,那么您将使用 localhost/log/index.php/job/index/php 获取参数

    要删除 index.php,您需要按照以下步骤创建 .htaccess 文件:

    1. 创建一个 .htaccess 文件,其中包含 index.php 文件的内容

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ index.php/$1 [L,QSA]
      
    2. 确保 apache 可以访问这个 .htaccess 文件。为此,请编辑 apache 配置文件。如果你使用ubuntu,那么它是/etc/apache2/sites-available/default,然后将AllowOverride none更改为AllowOverride all,用于目录和www目录。

         <Directory />
            Options FollowSymLinks
            AllowOverride all
         </Directory>
         <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride all
            Order allow,deny
            allow from all
         </Directory>
      
    3. 如果没有,则启用 mod rewrite,使用以下命令:

          `sudo a2enmod rewrite`
      
    4. 最后别忘了重启apache。

    希望这会有所帮助。

    【讨论】:

      【解决方案5】:

      用途:

      $param = $this->uri->segment(3);
      

      在您的文件夹中添加 .htaccess(您的情况是 "log"):

      RewriteEngine On
      RewriteBase /log/
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /log/index.php/$1 [L]
      

      【讨论】:

        猜你喜欢
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-24
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多