Answer the question
In order to leave comments, you need to log in
Russian-language CNCs in CodeIgniter?
Hello everybody!
I want to say right away that I can do CNC like site.tld/Ololo and site.tld/Atata and about this: code-igniter.ru/forum/post18174.html (do not take it as an advertisement of the resource, I have nothing to do with it ) is also aware. I have a more complex case.
I need a CNC like site.tld/fairy tales/tale-of-a-white-bull site.tld/stories/tales-of-time-years site.tld/novels/virgin-upgraded-lands (of course, all this was taken from the bulldozer for an example to show regularity).
If I enter something like this in the routes:
$route['tales'] = "skazki";
in skazki.php only index works correctly. I'm trying with $this->uri->segment(n) to pull out the "tale-of-a-white-bull", it doesn't work. Tried like this:
$route['tales'] = "skazki/show";
and so
$route['tales:/any'] = "skazki";
in any case throws on 404 page.
In principle, this is enough if the urls are formed like this:
site.tld/fairy tales/?q=fairy-tale-about-white-bull and in the controller inside the index`and catch via $_GET['q'].
You can go deeper along this path and include $config['enable_query_strings'] and as the index.php?c=controller&m=method documentation says, and then brazenly and cynically using mod_rewrite.
1) Maybe in the routes you need some kind of correct regular expression to work as intended, or am I misunderstanding something with setting up routes in CI?
2) If it’s still better to catch through ?q=… help with the Apache rewrite rule) Honestly - mod_rewrite is “pain is my ass hole” (c) Borat
3) If not 1 and not 2, what is the harm in using the 3rd point? In principle, under it and the rewrite rule, I can imagine what it will be, but I can’t imagine what architectural restrictions to expect from this solution. And maybe it's even better than the crutch I'm asking about in question 2?
Thank you in advance)
Added the next day:
rating -5, someone downvoted in karma, ICHSH no answer at all. It's a shame even ...
Added a day later:
Thanks to everyone who answered, minus, plus, in the end I figured it out!
I was ruined by thoughtlessness in the process of reading manuals.
The casket opened easier than I thought:
In routes:
$route['сказки/(:any)'] = "skazki/show/$1";<br/>
$need = $this->uri->segment(2, 0);<br/>
Answer the question
In order to leave comments, you need to log in
you can redirect all requests with Russian letters to the controller, which will parse them
$route['([-_.,a-z0-9а-я/]+)'] = "router/$1";
yes, and you also need to expand the Routes module, because there is no UTF support by default. Save the following in application/core/MY_Router.php
<?php
class MY_Router extends CI_Router
{
/**
* Parse Routes
*
* This function matches any routes that may exist in
* the config/routes.php file against the URI to
* determine if the class/method need to be remapped.
*
* @access private
* @return void
*/
function _parse_routes()
{
// Turn the segment array into a URI string
$uri = implode('/', $this->uri->segments);
// Is there a literal match? If so we're done
if (isset($this->routes[$uri]))
{
return $this->_set_request(explode('/', $this->routes[$uri]));
}
// Loop through the route array looking for wild-cards
foreach ($this->routes as $key => $val)
{
// Convert wild-cards to RegEx
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
// ADDED STRING
$uri = urldecode($uri);
// Does the RegEx match?
if (preg_match('#^'.$key.'$#ui', $uri))
{
// Do we have a back-reference?
if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#ui', $val, $uri);
}
return $this->_set_request(explode('/', $val));
}
}
// If we got this far it means we didn't encounter a
// matching route so we'll set the site default route
$this->_set_request($this->uri->segments);
}
}
?>
If it mod_rewrite
scares, then the simplest thing is ErrorDocument
.
In the file .htaccess
we write:
ErrorDocument 404 /404.php
and here you can play with this file:404.php
<?php
function robots($host)
{
header("$_SERVER[SERVER_PROTOCOL] 200 OK");
header("Content-Type: text/plain");
$host=strtolower($host);
echo "Host: $host\r\n";
exit();
}
function favicon($old=false)
{
header("$_SERVER[SERVER_PROTOCOL] 200 OK");
if ($old) header("Content-Type: image/x-icon"); else
header("Content-Type: image/vnd.microsoft.icon");
// "image/ico", "image/icon", "text/ico", "application/ico" => ERRORS!!
$favicon
="AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAA"
."AAAwAAAAAAAAAAAAAAAAAAAAAAAAAAEAgQA/IIEAPTy5AD8+vQA/P"
."r8AAQC/AD89uwA/P78AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
."AAAAAAAAAAAABEREQAAAAAREXIREQAAAREXNzcREAAREXd3d3cRAB"
."EXd3d3d3EBERd3d3dhERERd3cXcRERERF3cRd3EREVVXdVd3d1VVV"
."Vd1V3d3VVVVV3VXd3d1VQVVdVd3d3VQBVd1V3d3VVAAV3VXd3dVAA"
."AFVVVHd1AAAAAFVVVQAAD4HzsA4Ac7AMADAACAAQAAgAEAAAAAAAA"
."AAAAAAAAAAAAAAAAAAAAAAAAAAIABAACAAQAAwAMAAOAHAAD4HwAA";
echo base64_decode($favicon);
}
if (strpos($REQUEST_URI,"robots.txt"))
robots($HTTP_HOST);
if (strpos($REQUEST_URI,"favicon.ico"))
favicon(true);// favicon();
echo "<xmp>";
print_r($GLOBALS);
it gives a "competent" robots.txt
and pretty "favicon" favicon.ico
1. expand or reshuffle the
function _parse_routes() method in core/Router.php so that preg_match understands unicode /u add
2. in the config/config.php file
$config['permitted_uri_chars'] = 'az 0-9~%.: _\-';
expand with Cyrillic characters in your encoding
But in general, this is a very bad idea, very.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question