Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter中使用get变量时更改uri_Php_Codeigniter_Web Applications - Fatal编程技术网

Php 在codeigniter中使用get变量时更改uri

Php 在codeigniter中使用get变量时更改uri,php,codeigniter,web-applications,Php,Codeigniter,Web Applications,我刚刚开始学习Codeignator。我正在开发一个web应用程序,其中使用get变量,从数据库加载数据并显示它。 所以我的url看起来像: http://localhost/abc/book/?isbn=123456 我希望我的url看起来像 http://localhost/abc/book/123456 我认为在URI库和URI段的帮助下可以很容易地完成,但我必须严格使用GET方法。请建议解决方案,以便使用GET方法获得上述URL 以下是我的控制器的簿记方法: public funct

我刚刚开始学习Codeignator。我正在开发一个web应用程序,其中使用get变量,从数据库加载数据并显示它。 所以我的url看起来像:

http://localhost/abc/book/?isbn=123456
我希望我的url看起来像

http://localhost/abc/book/123456
我认为在URI库和URI段的帮助下可以很容易地完成,但我必须严格使用GET方法。请建议解决方案,以便使用GET方法获得上述URL

以下是我的控制器的簿记方法:

public function book()
{
    $slug = $this->input->get('isbn',TRUE);
    if($slug == FALSE)
    {
        $this->load->view('books/error2');
    }
    else
    {
        $data['book'] = $this->books_model->get_book($slug);

        if (empty($data['book'])) 
        {
            $data['isbn'] = $slug;
            $this->load->view('books/error',$data);
        }
        else
        {
        $data['title'] = $data['book']['title'];
        $this->load->view('templates/header',$data);
        $this->load->view('books/view',$data);
        $this->load->view('templates/footer');
        }
    }
}

也许我遗漏了什么,但是您可以使用get方法使用URI段向函数传递参数:

public function book($slug)
{
    if($slug == FALSE)
    {
        $this->load->view('books/error2');
    }
    else
    {
        $data['book'] = $this->books_model->get_book($slug);

        if (empty($data['book'])) 
        {
            $data['isbn'] = $slug;
            $this->load->view('books/error',$data);
        }
        else
        {
        $data['title'] = $data['book']['title'];
        $this->load->view('templates/header',$data);
        $this->load->view('books/view',$data);
        $this->load->view('templates/footer');
        }
    }
}

如所述:

如果URI包含两个以上的段,它们将作为参数传递给函数

例如,假设您有如下URI:

example.com/index.php/products/shoes/sandals/123
您的函数将被传递到URI段3和4(“檀香”和“123”):


如果您的唯一目的是不必更改html表单,那么我们为什么不编写一个小包装器呢

你只需要一条合适的路线就行了

class book extends MY_Controller{

    public function __construct()
    {

        parent::__construct();
        // If the isbn GET Parameter is passed to the Request 
        if($this->input->get('isbn'))
        {
            // Load the URL helper in order to make the 
            // redirect function available
            $this->load->helper('url');

            // We redirect to our slugify method (see the route) with the 
            // value of the GET Parameter
            redirect('book/' . $this->input->get('isbn'));
        }

    }

    public function slugify($isbn)
    {
        echo "Put your stuff here but from now on work with segments";
    }

}
现在是路线

$route['book/(:any)'] = "book/slugify/$1";
无论何时

它将被发送到

GET参数被传递给我们的slagify方法,您可以在其中处理URI段。HTML表单不需要触摸


但是,如果您坚持在脚本中处理GET参数,这当然不起作用。

您的意思是说您应该仅使用GET方法获取此值:
123456
,但您的URL不应包含
查询字符串
!同样,应该像传递参数一样传递该值???实际上,我得到了一个问题语句,其中指出:“页面URL应该像这样{{isbn number}”示例:isbn编号是一个GET变量。应该为GET变量进行适当的配置。获取isbn编号并显示所有书籍详细信息,包括与该isbn编号对应的书籍图像。“现在我有点搞不清楚GET变量到底是什么。以上问题是基于我对这个问题陈述的理解。。。我没弄错你的问题。。。!我正在研究它…抱歉,我有点困惑,就像我有一个表单,用户在其中输入一个get变量“isbn”,然后重定向到这个控制器的方法手册,然后是该部分的其余部分。若我将使用你们建议的方法,那个么我将如何获得“isbn”的值,即通过表单提交的变量。我明白你们的意思。看起来@Thomas David Plat的答案符合您的要求。简单明了!我试图用
.htaccess
来实现这一点,但还是伤了我的头!谢谢:)如果将构造函数代码重新定位到钩子中,这将更加有效。因此,您可以一次覆盖所有控制器,而不需要我目前所拥有的讨厌的开销:)这就是我所寻找的。非常感谢你!它抛出一个错误:“调用未定义的函数redirect()”我认为如果用户给出输入,比如456,那么它试图重定向到函数“book/456”,而函数“book/456”不可用,它抛出一个错误。正如您所说,我已经正确配置了routes.php。您需要加载url帮助程序。我总是自动加载url助手,因此我忘了在这里手动加载:)我只需编辑我的帖子。
$route['book/(:any)'] = "book/slugify/$1";