php 如何在codeigniter的索引函数中传递参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5545507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:46:29  来源:igfitidea点击:

How to pass parameters in index function in codeigniter

phpcodeigniter

提问by user691983

here's an example of url: http://www.example.com/search/search-keyword

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

I am trying to make this work, I removed the index.php using .htaccess, search is the controller and I want to pass a parameter in the index method.

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

this is currently what it looks like:

这是目前的样子:

class Search extends CI_Controller{

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

}

any suggestions? I can't seem to make it work

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

回答by Wesley Murch

  • You need the indexsegment http://www.example.com/search/index/search-keyword.
  • Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
  • Or you can look at remap
  • 你需要indexhttp://www.example.com/search/index/search-keyword
  • 或者你需要使用路由 $route['search/(:any)'] = 'search/index/$1';
  • 或者你可以看看remap

Remember not to trust user input, especially when you are throwing it into your url. The latest version of CI supports $_GETvariables now, so you may want to look into using that or flashdata. A searh term as simple as O' Brienwill give you a fatal error ("The URI you submitted has disallowed characters.").

记住不要相信用户输入,尤其是当你把它扔进你的 url 时。最新版本的 CI$_GET现在支持变量,因此您可能需要考虑使用那个或flashdata。一个简单的搜索术语O' Brien会给你一个致命的错误(“你提交的 URI 不允许使用字符。”)。

回答by aedwards

class Search extends CI_Controller{

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

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

}

You should then be able to access that as: /search/123123 :and the page would echo out "123123" or whatever you put in place of that.

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

回答by Alejo

function _remap($parameter){

        $this->_index($parameter);

}

Give it a try and tell us how it went.

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

回答by wgp

Most of these solutions will only work if you only have a single function called index()in your controller. If you have this in your routes:

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

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

With the above in your routes, what will happen is that your search function will work but then if you add any other functions to that same controller they'll all be redirected to /search/index/$1.

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

The only solution I can think of that allows you to use the URL you want while still being able to add any other functions to your search controller is to add a sort of messy conditional to your routes file. Here's what it will look like and what it does:

我能想到的唯一解决方案是允许您使用所需的 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'; }

$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'; }

What this is doing is basically saying "if the requested URL doesn't contain the name of any of my search controller functions then it must be meant for the index so we'll activate the route rule for index".

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

This will allow you to take requests for search/any_other_functions_you_havewithout issue and only activate the rule for hiding the index function when a request URI doesn't match any of them.

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

One side effect of this is that you'll never get a 404. For example, if someone enters a URL like "yourdomain.com/search/something" and they expect it to show a non-search result page they won't get a 404 alerting them to the fact that there is no page like that and instead the app will assume what they typed is a search term. However, it sounds like this isn't much of an issue for you and I don't see it being such a terrible thing for one controller to be unabe to return 404s.

这样做的一个副作用是你永远不会得到 404。例如,如果有人输入一个像“yourdomain.com/search/something”这样的 URL 并且他们希望它显示一个他们不会得到的非搜索结果页面一个 404 提醒他们没有这样的页面,应用程序会假设他们输入的是搜索词。但是,听起来这对您来说并不是什么大问题,我不认为一个控制器无法返回 404 是一件多么可怕的事情。

回答by John Mellor

You need to understand the way code igniter urls work, its basically like this:

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

http://www.mysite.com/{controller}/{function}

http://www.mysite.com/{控制器}/{功能}

So what your url is actually looking a function called "keyword" in your search controller.

那么您的网址实际上在您的搜索控制器中寻找一个名为“关键字”的函数。

You have multiple options. The easiest will be to simply change the url to:

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

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

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

Then this should work perfectly:

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

class Search extends CI_Controller{

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

}

If you really want to use the url as you had it, you can use the same snippet of code as above but also open up "application/config/routes.php" and add this to the bottom.

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

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

Alternatively if you want to continue using the index function you can change it to this

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

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

And change your class to something like this:

并将您的课程更改为如下所示:

class Search extends CI_Controller{

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

}

Don't forget to update your answer if you figure it out yourself or accept somebodies answer if it answers it :)

如果您自己弄明白了,请不要忘记更新您的答案,或者如果它回答了就接受某人的答案:)

回答by Roberto C.

This is my solution:

这是我的解决方案:

From CI userGuide

来自 CI 用户指南

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

So I've created my controller in this way:

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

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);
    }
}
}

Hope this can be helpful to someone...

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

BUG: if you call /shortener _remap function pass "index" to index() function...it can be solved adding an if condition or something else

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

回答by user691983

I've just started CI and here was my solution and implemented it on my website. So far it works for me and my search queries have been indexed by google as links

我刚刚开始 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
    }

   }

回答by Peter

You should use the CI URI Class. http://codeigniter.com/user_guide/libraries/uri.html

您应该使用 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);
     }

}

回答by jfoucher

This easiest way is just to use uri segments:

这种最简单的方法就是使用uri 段

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

Or like Madmartigan said, use routes, which is probably the better way of doing this.

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

回答by Gim

Thank you. This code works for me. If parameter is a number

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

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

However, If the parameter is a string (http://[::1]/note/getnote/index/fg=> http://[::1]/note/getnote/fg). I use _remap() like this code.

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

<?php

class Getnote extends CI_Controller {

类 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');
}

}

}