Quick help - CodeIgniter routing - php

Need a quick hand on setting custom route for my project.
the code works without any reoute definition as
http://myblog.local/posts/categories/show/1
but I want to access it as following;
http://myblog.local/posts/1
and I've changed my route as following;
$route['posts/(:any)'] = 'posts/categories/show/$3';
But my route declaration seems not working, please help me where i am doing the mistake

$route['posts/(:num)'] = 'posts/categories/show/$1';
Because you have only 1 segment (the first) to match: $1
And the segment must be a number: (:num)
(:any) will work too.

$route['posts/(:num)'] = 'posts/categories/show/$1';
also in config file
make it blank
$config['index_page']='';
hope this might work.

Related

I want to remove controller as well as function name from url

I need help to rewrite url and remove controller as well as function name from url given url is my current uel.
URL :http://localhost/example/Loadercontrol/detail/200
I want this type of url
URL : http://localhost/example/name (replace id with name).
I already try rewrite url as well as
$route['example'] = 'localhost/example/Loadercontrol/detail';
but it's not working. I've really tried to fix the issue. Please help me.
Try like this..
$route['example/(:num)'] = 'example/Loadercontrol/detail/$1';
For details see more here...https://www.codeigniter.com/userguide3/general/routing.html#wildcards
Wildcards are actually aliases for regular expressions, with :any being translated to [^/]+ and :num to [0-9]+, respectively.

Extracting GET Parameters in CodeIgniter from the URL

URL mapping can be done at .htaccess and we can edit routes.php. Learning PHP/Codeigniter, i posted this question. CodeIgniter Mod Rewrite Rules and the Controller
routes.php worked for me, and the next thing i'd like is to have access to the GET parameters in my URL. To elaborate on what i'm actually trying to do:
Case 1:
RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/show?personName=$1&place=$2 [L]
This doesn't work at the .htaccess as in the above question, but I can get that working in the routes.php
$route['([a-z]+)/at/([a-z]+)'] = "person/show/$1/$2";
If i try the routes.php as
$route['([a-z]+)/at/([a-z]+)'] = "person/show?personName=$1&place=$2";
I end up with a CodeIgniter 404. Is this fundamentally incorrect?
This works fine as is:
person/show?personName=chinmay&place=kino
Case 2:
URL which I want to expose:
chinmay?place=kino
where place is shown as a GET param. I would like to handle this, and knowing as much about routes.php-
$route['([a-z]+)\?place=([a-z]+)'] = "person/show/$1/$2"
This gives a CI 404 as well.
Passing URI Segments to your Functions
or
GET parameters in the URL with CodeIgniter
You could use:
<?php
function myFunction($param1, $param2){
//code
}
?>
And access it like: .../controller/myFunction/param1/param2

Codeigniter dynamic variable routing

I am looking to dynamically re-route urls based on a parameter within the uri segment. For example if I have the following url:
domain.com/account/message/action/view
How could I go about creating a route that checked to see if the uri contained "action" and if so re-route to the value of the function + '_' + value of action?
domain.com/account/message_view
I've been reading through the routes documentation provided by ellislab and am still not quite sure how to go about doing something like this or if it would be better to use server rewrite rule? Any information greatly appreciated!
You would likely want to use the regular expressions availability in routes. Something along the lines of:
$route['([a-z]+)/action/([a-z]+)'] = "$1_$2";
Of course, that may not be exactly what you are looking for, but it should get you started.

Parenthesis issue in codeigniter url

Few of my project urls contains the parenthesis and it is a codeigniter project. Unfortunately I cant remove them.
So I am writing following rules in my route.php of config folder:
$route['abc-(def)'] = 'locations/index/12492';
$route['abc-%28def%29'] = 'locations/index/12492';
$route['404_override'] = 'home/pagenotfoundhandler';
So when I am trying to access URL http://mydomain.com/abc-(def) or http://mydomain.com/abc-%28def%29 it takes me to home/pagenotfoundhandler, while it should take me to location controller and index method. Please suggest how to deal with parenthesis in codeigniter.
The parentheses are a part of the regex pattern, so try escaping them if you want to use them as ascii characters like this:
$route['abc-\(def\)'] = 'locations/index/12492';
And one more place to look:
There should be a allowed uri characters setting in config.php for your codeigniter system. check there if parentheses are allowed.
Alternatively, you can disable $config["csrf_protection"] to skip url xss cleaning.
Well I guess that is because the page http://mydomain.com/abc-(def) which is supposed to redirect to locations/index/12492 can not be found! so a 404 - Page not Found error takes place, which of course as you have defined takes you to home/pagenotfoundhandler! make sure the path exists or even why do not you give the complete path to see if it works!

Redirect & Route wildcard URL - with underscores to hyphens

I'm using CodeIgniter and I needed to redirect several urls with underscores to their equivalent with hyphens.
/some-controller --> /some_controller
I've partially solved this issue by tweaking the config/routes.php file.
The thing is :
How would I expand that to a Controller's function WITH a parameter.
Let's say I've got a controller some_controller (some-controller redirects to some_controller) and a function func in it (optionally taking a param).
$route['some-controller'] = 'some_controller';
$route['some-controller/func'] = 'some_controller/func';
This works. But, what if I have some-controller/func/someparam. (and someparam can be anything).
How could this redirect be implemented?
Basically what I need is a redirection from :
some-controller/func/*
to
some_controller/func/*
Any ideas?
Hint :
I don't need anything complicated like this one (
How to replace underscores in codeigniter url with dashes? ).
Hadn't searched enough - I admit it (it's one of those issues that always seem more complicated than they should be) :
http://codeigniter.com/user_guide/general/routing.html
$route['some-controller/func/(:any)'] = "some_controller/func/$1";
Or you could use (:num), if what you're expecting is a number...

Categories