【问题标题】:How to pass parameters in index function in codeigniter如何在codeigniter的索引函数中传递参数
【发布时间】:2011-07-29 13:34:54
【问题描述】:

这里是一个 url 的例子: http://www.example.com/search/search-keyword

我正在尝试完成这项工作,我使用 .htaccess 删除了 index.php,搜索是控制器,我想在 index 方法中传递一个参数。

这是目前的样子:

class Search extends CI_Controller{

    function index($param){
        echo $param;
    }

}

有什么建议吗?我似乎无法让它工作

【问题讨论】:

  • 你最后找到解决办法了吗?

标签: php codeigniter


【解决方案1】:
  • 您需要indexhttp://www.example.com/search/index/search-keyword
  • 或者你需要使用路由$route['search/(:any)'] = 'search/index/$1';
  • 或者你可以看看remap

请记住不要相信用户输入,尤其是当您将其放入您的网址时。最新版本的 CI 现在支持 $_GET 变量,因此您可能需要考虑使用它或 flashdata。像 O' Brien 这样简单的搜索词会给您一个致命错误(“您提交的 URI 包含不允许的字符。”)。

【讨论】:

  • 不可以对查询参数进行URLencode,然后在controller中解码吗?
  • @pat:不,您仍然会遇到错误。但是,您可以加密参数 - 但这毫无意义。您也可以使用会话或发布数据,但它不会显示在 url 中。
  • ya 第一个选项对我来说真的很有效,只需在控制器名称后指定函数名称(就像我们经常调用其​​他函数一样。例如href="<?php echo base_url()."usage/index/$selected_next_date"; ?>"
  • 这应该被标记为正确的解决方案。尽管在 CodeIgniter 4 中,在 Routes.php 中添加路由的语法是 $routes->add('search/(:any)', 'search/index/$1'); 。这正确回答了原始问题,原始问题的代码无需修改即可工作。事实上,URI 将按预期使用 http://www.example.com/search/{$param}
  • 对不起,我有一个错误:这不会像预期的那样工作 $routes->add('search/(:any)', 'search/index/$1'); 而你必须使用 $routes->add('search/(:any)', 'search/$1'); 否则它将作为参数传递“索引”。显然没有意义,但我想这是因为从 CodeIgniter URI 中删除“index.php”的机制
【解决方案2】:
class Search extends CI_Controller{

    function _remap($param) {
        $this->index($param);
    }

    function index($param){
        echo $param;
    }

}

然后您应该能够以以下方式访问它:/search/123123:页面将回显“123123”或您放置的任何内容。

【讨论】:

  • 这很好,但我想知道这是否是正确的做法
【解决方案3】:
function _remap($parameter){

        $this->_index($parameter);

}

试一试,告诉我们进展如何。

【讨论】:

    【解决方案4】:

    大多数这些解决方案只有在您的控制器中只有一个名为 index() 的函数时才有效。如果你的路线中有这个:

    $route['search/(:any)'] = 'search/index/$1';
    

    在您的路线中使用上述内容后,您的搜索功能将起作用,但如果您将任何其他功能添加到同一控制器,它们将全部重定向到/search/index/$1

    我能想到的唯一解决方案允许您使用所需的 URL,同时仍然能够向搜索控制器添加任何其他功能,即在您的路由文件中添加一种杂乱的条件。以下是它的外观和作用:

    $request = $_SERVER['REQUEST_URI']; // Only add this for readability if(!strpos($request, 'search/another_function') || !strpos($request, 'search/more_functions')) { $route['search/(:any)'] = 'search/index/$1'; }

    这基本上是在说“如果请求的 URL 不包含我的任何搜索控制器函数的名称,那么它必须用于索引,因此我们将激活索引的路由规则”。

    这将允许您毫无问题地接受对search/any_other_functions_you_have 的请求,并且仅在请求 URI 与其中任何一个都不匹配时激活隐藏索引功能的规则。

    这样做的一个副作用是您永远不会收到 404。例如,如果有人输入“yourdomain.com/search/something”之类的网址,并且他们希望该网址显示他们赢得的非搜索结果页面'不会收到 404 提醒他们没有这样的页面的事实,而是应用程序将假定他们键入的内容是搜索词。但是,这听起来对您来说不是什么大问题,而且我认为一个控制器无法返回 404 并不是一件可怕的事情。

    【讨论】:

      【解决方案5】:

      您需要了解代码点火器网址的工作方式,基本上是这样的:

      http://www.mysite.com/{控制器}/{函数}

      所以你的网址实际上是在你的搜索控制器中寻找一个名为“关键字”的函数。

      您有多种选择。最简单的方法是将网址更改为:

      http://www.mysite.com/search/result/keyword

      那么这应该可以完美地工作:

      class Search extends CI_Controller{
      
      function result($param){
       echo $param;
      }
      
      }
      

      如果你真的想使用你拥有的 url,你可以使用与上面相同的 sn-p 代码,但也可以打开“application/config/routes.php”并将其添加到底部。

      $route['search/(:any)'] = "search/result";
      

      或者,如果您想继续使用索引功能,您可以将其更改为此

          $route['search/(:any)'] = "search/index";
      

      把你的班级改成这样:

      class Search extends CI_Controller{
      
        function index($param=FALSE){
          if($param == FALSE) {
            //Show search box
          } else {
            //Show search results
          }
        }
      
      }
      

      如果您自己弄清楚,请不要忘记更新您的答案,或者如果有人回答,请接受某人的回答:)

      【讨论】:

      • 这真的很酷,它有效。但是如何访问传递给索引函数的参数呢?
      【解决方案6】:

      我刚刚开始 CI,这是我的解决方案并在我的网站上实施。到目前为止,它对我有用,我的搜索查询已被谷歌索引为链接

      Class Search extends CI_Controller{
          function index(){
      
          $search_item = $this->input->post('search_box'); // this is from the input field
      
          redirect(base_url()."search/q/".url_title($search_item));
      
          }
      
      
      
       //  i've redirected it to the "search" controller then :
      
           function q($search_item = null){// process search
          // load the view here with the data
          }
      
         }
      

      【讨论】:

        【解决方案7】:

        这是我的解决方案:

        来自 CI 用户指南

        public function _remap($method)
        {
            if ($method == 'some_method')
            {
                $this->$method();
            }
            else
            {
               $this->default_method();
            }
        }
        

        所以我以这种方式创建了我的控制器:

        class Shortener extends CI_Controller {
        function shortener() {
            parent::__construct();
        }
        
        function index($getVar) {
            echo "Get var: " . $getVar;
        }
        
        function config() {
            echo "another function";
        }
        
        function _remap($method) {
            if($method == "config") {
                $this->$method();
            }
            else {
        
                $this->index($method);
            }
        }
        }
        

        希望这对某人有所帮助...

        BUG:如果您调用 /shortener _remap 函数将“索引”传递给 index() 函数...可以通过添加 if 条件或其他内容来解决

        【讨论】:

          【解决方案8】:

          您应该使用 CI URI 类。 http://codeigniter.com/user_guide/libraries/uri.html

          
          class Search extends CI_Controller{
          
               function index($param){
                  //echo the search-keyword
                  echo $this->uri->segment(2);
               }
          
          }
          

          【讨论】:

          • 这不起作用,因为没有调用索引函数,它正在寻找不存在的函数'search-keyword'
          • 将 uri 段 2 声明为构造中的搜索关键字怎么样?
          【解决方案9】:

          这个最简单的方法就是使用uri segments:

          class Search extends CI_Controller{
            function index(){
              $param=$this->uri->segment(2);
              echo $param;
            }
          }
          

          或者像 Madmartigan 所说的那样,使用路由,这可能是更好的方法。

          【讨论】:

          • 这不起作用,因为没有调用索引函数,它正在寻找不存在的函数'search-keyword'
          • 很对,反正还是要加个路由,所以我觉得最好还是用路由方法
          • 您也可以使用 php5 __call,类似于使用 CI 的 _remap。我个人喜欢保持我的路线干净和精简,所以我喜欢那些只做一两件事的简单控制器的方法。
          【解决方案10】:

          我还必须检查是否传递了某个变量 - 所以这是我采用的解决方案。

          <?php
          
          defined('BASEPATH') OR exit('No direct script access allowed');
          
          class Story extends CI_Controller{
          
              function story() {
                  parent::__construct();
              }
          
              function _remap($parameter){
                  $this->_index($parameter);
              }  
          
              public function _index($story_id) {        
                  if($story_id == 'index'){
                      //No variable passed
                      redirect('auth', 'refresh');
                  }else{
                      $data['title'] = "Story";
                      $this->load->view('story', $data);                
                  }
              }
          
          }
          

          希望这对某人有所帮助!

          【讨论】:

            【解决方案11】:

            谢谢。这段代码对我有用。如果参数是数字

            Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
            

            但是,如果参数是字符串(http://[::1]/note/getnote/index/fg => http://[::1]/note/getnote/fg)。我像这段代码一样使用 _remap()。

            <?php
            

            Getnote 类扩展 CI_Controller {

            public function __construct()
            {
                parent::__construct();
                $this->load->library('image_lib');
            }
            
            public function index(){
            
            }
            
            public function _remap($keyname)
            {
                $this->load->model('Laydb');
                $data = $this->Laydb->getNote($keyname);
                $proListArray['patitle'] = "Edit notepad";
            
                $this->load->view('top', $proListArray);
                $this->load->view('editblog', $data);
                $this->load->view('bottom');
            }
            

            }

            【讨论】:

            • 与@Bill 的回答有何不同?
            【解决方案12】:
            class Search extends CI_Controller{
            
                function index($param){
                    echo $param;
                }
            
            }
            

            在 routes.php 中

            $routes['search/(:any)'] = 'search/index/$1';
            

            就是这样

            【讨论】:

              【解决方案13】:

              我终于找到了一种解决方法,因为我不能简单地将帖子变量放入 URL。

              我所做的是创建另一个函数,然后将其重定向到该函数。

               class Search extends CI_Controller{
                 function index(){
                      $search_item = $this->input-post('search_item');
                      redirect("search/q/".url_title($search_item));
                }
              
                function q($key){
                  // process search
                }
               }
              

              【讨论】:

              • 这会改变搜索词,这是不可取的。如果我搜索Hello World!,您将返回hello-world的结果
              猜你喜欢
              • 2019-06-27
              • 1970-01-01
              • 1970-01-01
              • 2011-08-19
              • 1970-01-01
              • 2019-05-25
              • 1970-01-01
              • 2013-03-18
              • 2014-05-05
              相关资源
              最近更新 更多