Codeigniter Alternative Get Variables

I was looking for a way to pass the "GET" variables to codeigniter and ended up with this: link text

I am wondering how to implement it.

For instance:

www.website.com/query will provide me with every entry in the database.

Usually i would have

www.website.com/query/?id=5 to get an equivalent entry.

when I try to do it in CI way:

www.website.com/query/id/5

I get a 404 error because I am looking for a class called id and cannot find it.

Is there a way to get a step by step way to do this?

thank.

+3
source share
3 answers

Two good ways to accomplish this using methods developed by Codeigniter developers.

OPTION ONE:

"id" , , URI (), .

/[controller]/[method]/[value]:

http://www.website.com/query/index/5

"id" .

Class Query extends Controller {
...

    // From your URL I assume you have an index method in the Query controller.
    function index($id = NULL)
    {
        // Show current ID value.
        echo "ID is $id";
        ...
    }
    ...
}

:

, = > URI .

/[controller]/[method]/[key1]/[val1]/[key2]/[val2]/[key3]/[val3]:

http://www.website.com/query/index/id/5/sort/date/highlight/term

URI ( "id" ) = > uri_to_assoc($segment) URI.

Class Query extends Controller {
...

    // From your code I assume you are calling an index method in the Query controller.
    function index()
    {
        // Get parameters from URI.
        // URI Class is initialized by the system automatically.
        $data->params = $this->uri->uri_to_assoc(3);
        ...
    }
    ...
}

, URI, .

$data->params URI:

Array
(
    [id] => 5
    [sort] => date
    [highlight] => term
)

:

, , = > . , , .

/[controller]/[method]/[id]/[key1]/[val1]/[key2]/[val2]:

http://www.website.com/query/index/5/sort/date/highlight/term

URI 4- ( "" ) = > uri_to_assoc($segment) URI.

Class Query extends Controller {
...

    // From your code I assume you are calling an index method in the Query controller.
    function index($id = NULL)
    {
        // Show current ID value.
        echo "ID is $id";

        // Get parameters from URI.
        // URI Class is initialized by the system automatically.
        $data->params = $this->uri->uri_to_assoc(4);
        ...
    }
    ...
}

$id ID, $data->params URI:

+6

GET, -:

test.com/query/id/4

:

$query->id($id);

, - controllers CI.

POST CI.

+1

Use $ this-> uri-> uri_to_assoc (2) 2 - this is the offset when you start your associative array of segments in the second segment. You will also need a route to create / query a map for the controller and method (unless you do this in the index () method).

So this url:

/ request / identifier / foo / key / bar

can be read using:

$get = $this->uri->uri_to_assoc(2);

echo $get['id']; // 'foo'
echo $get['key']; // 'bar'

This is not great, but it works.

0
source

Source: https://habr.com/ru/post/1776988/


All Articles