📜 ⬆️ ⬇️

CNC + GET

Hello. As is known from the CI documentation, we can set controllers and actions via GET parameters (for example? C = stationery & act = contacts), or use CNC (for example stationery / contacts), but then we cannot pass parameters to the url get. In my opinion, it is not entirely correct to reject get, since we can not make a universal pager (who faced, I think, will understand), nor specify the sorting result. Another unsolved task for me was to do an advanced search. In theory, all search parameters are specified in the URL, but some fields may be specified, some are not. Of course, this all costs, but the most understandable way would be to use get parameters.

To solve this problem, you need to extend the class CI_URL. Then I will give the class code, if something is done not optimally or not correctly, I will be glad to hear any criticism.

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

class MY_URI extends CI_URI {

var $_get_params = array();

function _fetch_uri_string() {

parse_str($_SERVER[ 'QUERY_STRING' ], $ this ->_get_params);

$_GET = array();
$_SERVER[ 'QUERY_STRING' ] = '' ;

parent::_fetch_uri_string();

}

function getParam($key) {
if (isset($ this ->_get_params[$key])) {
return $ this ->_get_params[$key];
} else {
return false ;
}
}

function getParams() {
return $ this ->_get_params;
}

}


* This source code was highlighted with Source Code Highlighter .

')
UPD. Improved input parameter parser. Thank you homm

The code should be clear. Now give an example of use. Suppose we need to make a pager for the product catalog.

Class Catalog extends Controller {



function production() {

$current_page = $ this ->uri->getParam('page');

/* */

}

}

* This source code was highlighted with Source Code Highlighter .


The URL for calling this action is: / catalog / produnction /? Page = 2

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


All Articles