URI Routing With Dynamic value - php

This is my code in routes.php
$route['default_controller'] = "admin";
$route['(:any)'] = $route['default_controller']."/index/";
This is my url:
http://myserver.net/visio/jklmn
But i cant to get the value in index() in admin controller.
I want to get the value jklmn in admin controller.If there is any mistake in my routing code.
This is my index() code;
function index($key = ""){
if(isset($key)){
$newkey = $key;
$data['key'] = $key;
$this->load->view('index',$data);
}else{
redirect('admin/index_login');
}
}
When i taking above link in browser i get the error message below:
Not Found
The requested URL /visio/jklmn was not found on this server.

Use this routing rule:
$route['(:any)/(:any)'] = $route['default_controller']."/index/$2";
which will match a URL with 2 segments (each containing any character) and pass the second match as $2.
You can also pass the first match, just use $1.

Related

Where is define codeigniters rewrite rules for recognizing controller,method,id in segment?

how codignitetr recognzes controllername,methodname,id from url like:
https://stackoverflow.com/controllername/methodname/id
The most used method for this is CI_URI->segments();
Read more here: https://www.codeigniter.com/userguide3/libraries/uri.html#CI_URI::segment
So, each / after your host be different segment.
For example:
//http://stackoverflow.com/controllername/methodname/id
echo $this->uri->segment(1); // controllername
echo $this->uri->segment(2); // methodname
echo $this->uri->segment(3); // id

Delete URI Segment in Codeigniter

I want to remove some url in CI
if any echo like:
www.blabla.co/content/5DRwA/6Yt54/bla-bla
so, replace to:
www.blabla.co/content/bla-bla
nb: 6Yt54 and 5DRwA is a random value
How I remove URI Segment 2 from behind like that?
How can I solve it?
Try the uri class. First retrive uri with:
$array = $this->uri->segment_array();
Then remove second segment:
unset($array[2]);
Unfortunately your url doesnt seem to follow CI convention (you could use then $this->uri->assoc_to_uri($array)). So iterate over array to create uri.
$my_uri = ''; //better name it $my_uri not to conflict with $this->uri
foreach ($array as $value) {
$my_uri.= '/'.$value;
}
And then you can use your new uri f.e. to redirect:
redirect($uri);

Codeigniter Make URL SEO FRIENDLY

My current URL is like:
www.hostname.com/get_city/
(this url is already shorten by using routes)
Here get_city is my method name and now what i exactly want is...
1)remove controller name from url
2)pass selected city value from dropdown and set in URL as a parameter
So,Required url is: www.hostname.com/california OR www.hostname.com/newjersey
NOTE:I know how to use routes but in such case how to make dynamic URL?!
AND please don't give me direct reference of ellislab docs because i have already tried those things
For dynamic route in codeigniter:
try like this:
in your routes.php file copy and paste these codes:
require_once (BASEPATH . 'database/DB' . EXT);
require_once (BASEPATH . 'helpers/url_helper' . EXT);
require_once (BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();
$query = $db -> get('news');
$result = $query -> result(); //result return all records
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> subject)));
$route[$string] = "controller/news_details/$row->id";
}
so you can , change $string with any string that you want.
then try type new url and see Routes will work fine.
NOTE:.htaccess file must be removing index.php in url
hope this help.
You can try with the method "_remap".
Check the official documentation for more info about the behavior of the function:
http://www.codeigniter.com/user_guide/general/controllers.html
Regards.
You need to config routes in application/config/routes.php
After existing routes add
$route['([a-z]+)'] = 'controller_name/method_name/$1';
But this overwrite all routes and before this route you need to declare all routes for you controllers
$route['product/:any'] = 'product/$1';
$route['catalog/:any'] = 'catalog/$1';
and after
// this route be used when previous routes is not suitables
$route['([a-z]+)'] = 'controller_name/method_name/$1';
DOCS:
http://code-igniter.ru/user_guide/general/routing.html
CI2: URI Routing
CI3: URI Routing

Forcing Routing in codeigniter using regex

i have following route in route.php in codeigniter
$route['^(?!login|signup|autos|jobs|jobwanted|admin).*'] = "pages/bpages";
it is redirecting everthing to pages/bpages except (login|signup|autos|jobs|jobwanted|admin )
eg www.mysite.com/sohailanwarpk
as sohailanwapk is not in (autos|jobs|jobwanted|admin) it will redirected to pages/bpages
the problem i am facing is in that condition if my url is like
eg www.mysite.com/autosss
eg www.mysite.com/jobs123
it should direct autosss or jobs123 to "pages/bpages"; but its not,so how can i exaclty match (autos|jobs|jobwanted|admin) so that everything else will be redirected.
Split it into 2 routes:
$route['^(?!(login|signup|autos|jobs|jobwanted|admin)).*'] = "pages/bpages";
$route['^(login|signup|autos|jobs|jobwanted|admin)[^\/].*'] = "pages/bpages";
Just write 2 rules:
$route['^(login|signup|autos|jobs|jobwanted|admin)$'] = '$1';
$route['(:any)'] = 'pages/bpages';

Redirect depending on URI Segment

I am using Codeigniter and want to have SEO-friendly URLs. There will be 2 types of URI segments, http://www.domain.com/view/193847 and http://www.domain.com/view/193847-canon-5d-mark-iii.
If the first URL is used, function view_mini is called and passed the product id 193847. If the 2nd one is used, function view_full will be called and passed the same product id 193847.
Problem: How can I differentiate between the 2 URLs? Or is this an inferior approach to solve the problem?
PHP How should the if condition be structured?
function view($pid) {
if($this->uri->segment(2) == something) {
$this->view_mini($pid);
} else {
$this->view_full($pid);
}
}
function view_mini($pid) {
// ...
}
function view_full($pid) {
// ...
}
EDIT
I am using URL routing to route http://www.domain.com/controllername/view/1234 to http://www.domain.com/view/1234
you can use the regular expression to check the segment if it has anything other than numbers, then you can execute the view that you want for example
$pattern = '\d[0-9][^a-zA-Z]';
$url = $this->uri->segment(2);
if(preg_match($pattern,$url))
{
//this will match only the numbers
$this->view_mini($pid);
} else {
$this->view_full($pid);
}
i hope this will help ..
Is there any definitive structure to the different URLs?
ie.
http://www.domain.com/view/[numbers]-[text]
If so, you could test the URL for that dash between the numbers and the dash?
Edit: un-tested
$route["view/(\d+)"] = "class/view_mini/$1";
$route["view/(\d+)-([\w'-]*)?/g"] = "class/view_full/$1/$2";
Use
$result = explode('-', $this->uri->segment(1));
If(isset($result[1]))
{
// show full view
} else {
// show mini view
}
$pid will always be $result[0]

Categories