changing url displayed to user in codeigniter - codeigniter

I don't know if this is strange request or not, but I have a page with users profiles displaying. Each user has unique username of course and those are included in links to associated with their names. when clicked it goes to their profile:
profile/profile_guest/username
When the user clicks on one of the profiles, it goes to user's profile and url in the address bar is:
http://domain.com/profile/profile_guest/ahlam
What I want to achieve is to configure routes.php to handle this. so once clicked, it goes to profile page but what shows in URL address bar is :
domain.com/ahlam
To do that, I tried :
$route['someTest'] = "profile/profile_guest/$1";
Fair to say that it didn't work and still the whole URL displayed. What can I do to fix this?
Thanks all

You should use something like
$route['sometest/(:any)'] = "your/link/$1";
The '$1' is like a parameter you gonna receive from the '(:any)' part.

//site.com/john = "profile/profile_gest/john"
$route['([a-z0-9\-_]+)'] = "profile/profile_gest/$1";
//where $1 = "john", so "john" respects the pattern set below
But CI Routing will understand any URI match this pattern like "index", "post", ... is a routing for the profile controller.
TO prevent that you can add a specification to the url, for examples:
//site.com/john.html
$route['([a-z0-9\-_]+)\.html'] = "profile/profile_gest/$1";
//site.com/user-john
$route['user\-([a-z0-9\-_]+)'] = "profile/profile_gest/$1";
Or
if you are sure that any usernames will not contest with an another pages
always put this rules at the bottom, in order to that CI will check all other routing rules before this one.
Note: Before that your htaccess have to be set to remove index.php from your urls.
CI URI Routing
Regular expression

Related

Codeigniter search?

This project is something like a social networking site built on codeigniter.
Here my default controller is MyController.php (which loads the login page)
and say my domain is aravind.com
In my application each user will have a unique id.
and what my requirement is, the particular page of the user should get opened when
the unique_id of the user is given immedetily after the domain name.
ie, aravind.com/123 should open the user page whose unique id is 123.
I know this can be acheived by placing a controller and a function in between the
domain and the unique_id, (like : aravind.com/Search_class/search_func/123).
But by doing so the url of the particular user becomes lengthy which is not acceptable.
So I require a logic to sort this issue.
Note:
I have other classes and folders in my controller package, SO the required soln should be
wise enough to diferentiate my classes and folders in the controller package with the UniqueID.
So for differentiating I can say like the classes and folder name will be of continuous String
Where UniqueID starts with an underscore (_).
The same thing is done in facebook, like if we type facebook.com/aravind.pillai.983, it will open my account,
where as if the url goes like facebook.com/settings it will open the settings page for
the user who have an active session.
Set route["404_override"] to one of your custom class (say Search.php).
So wheneven a user tries to enter with an invalid url it will be redirected to Search.php.
Here using $this->uri->segment(1) you will get the value of the first uri segment.
You can write a logic there to identify whether the user entered a valid unique_id or not.
Now you got the uri value in one class (Search.php).
You should use routing where you can set every routes you want. In APPPATH.'config/routes.php' file add this:
$route['(:any)'] = 'Search_class/search_func/$1';
You should read documentation about routing and check other examples.
Also, pay attention that application will always check for Search_class/search_func/$1 in first URI segment so you need to put this rule at the end of routes.php file. Before that rule you need to set other rules like:
$route['about'] = 'pages_c/show/about';
$route['contact'] = 'pages_c/show/contact';
// then after all declared routes, you can set wild card placeholder
$route['(:any)'] = 'Search_class/search_func/$1';

Codeigniter routing if not

I want to configure codeigniter routing something like this
This is working good
$route['login/(:any)'] = "login/index/$1";
$route['profile/(:any)'] = "profile/index/$1";
But i want to do this
if not = login or profile then i want to do this
$route['not(login or profile)'] = "home/index/$1";
This code is not right so please help me by give me code for this.
i have 3 controller they are
1. home
2. profile
3. login
Now if i go with this
domain.com/profile
then i want run profile controller, and if i go with
domain.com/profile/john
i need go get this name john from my controller
domain.com/profile/john/edit
i also need to get this john and edit from controller.
Now if i go with this
domain.com/login
then i want run profile controller, and if i go with
domain.com/login/best
i need go get this name best from my controller
domain.com/login/best/sure
i also need to get this best and sure from controller.
And if i go with
domain.com/
or domain.com/bestoffer
or domain.com/others
or domain.com/blabla
anything after domain without profile and login i need to run Home controller
That's all
Precedence is important here, your routes will be parsed from top to bottom, and the first match it finds, apply to routing,
So, lets say your route file looks like,
$route['login/(:any)'] = "login/index/$1";
$route['profile/(:any)'] = "profile/index/$1";
$route['(:any)'] = "controller/index/$1";
So, third rule here will be parsed as it is not login/(:any) and not profile/(:any).
To get parameters in your controller,
echo $this->uri->segment(1);
Put third rule in your route file at last, and segment block in controller to see it in effect.
Try this one.
$route['(:any)'] = "home/index/$1";

How can I shorten routes in Codeigniter for certain requests?

I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..
also this product view URL website.com/product/details/13/honda-accord-red-car
and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!
I cannot find correct instructions on what I am doing wrong??
In Routes.php you need to create one like so
$route['mycar'] = "controller_name/function_name";
So for your example it would be:
$route['honda-red-car] = "category/view/honda-red-car";
Take a look into the URI Routing part of the user guide.
If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.
If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!
Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:
$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';
If you want to generalize everything that starts with the honda- string you do:
$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint
These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.
Be careful with the patterns, if they are too general they might catch every request coming in, so:
$routes['(.*)'] = 'product/details_by_slug/$1';
While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.
These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.

Change CodeIgniter route for static pages and auth login [duplicate]

I have a problem with Codeigniter routes. I would like to all registered users on my site gets its own "directory", for example: www.example.com/username1, www.example.com/username2. This "directory" should map to the controller "polica", method "ogled", parameter "username1".
If I do like this, then each controller is mapped to this route: "polica/ogled/parameter". It's not OK:
$route["(:any)"] = "polica/ogled/$1";
This works, but I have always manually entered info in routes.php:
$route["username1"] = "polica/ogled/username1";
How do I do so that this will be automated?
UPDATE:
For example, I have controller with name ads. For example, if you go to www.example.com/ads/
there will be listed ads. If you are go to www.example.com/username1 there are listed ads by user username1. There is also controller user, profile, latest,...
My Current routes.php:
$route['oglasi'] = 'oglasi';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';
I solved problem with this code:
$route['oglasi/(:any)'] = 'oglasi/$1';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';
Regards, Mario
The problem with your route is that by using :any you match, actually...ANY route, so you're pretty much stuck there.
I think you might have two solutions:
1)You can selectively re-route all your sites controller individually, like:
$route['aboutus'] = "aboutus";
$route['where-we-are'] = "whereweare";
//And do this for all your site's controllers
//Finally:
$route['(:any)'] = "polica/ogled/$1";
All these routes must come BEFORE the ANY, since they are read in the order they are presented, and if you place the :any at the beginning it will happily skip all the rest.
EDIT after comment:
What I mean is, if you're going to match against ANY segment, this means that you cannot use any controller at all (which is, by default, the first URI segment), since the router will always re-route you using your defined law.
In order to allow CI to execute other controllers (whatever they are, I just used some common web pages, but can be literally everything), you need to allow them by excluding them from the re-routing. And you can achieve this by placing them before your ANY rule, so that everytime CI passed through your routing rules it parses first the one you "escaped", and ONLY if they don't match anything it found on the URL, it passes on to the :ANY rule.
I know that this is a code verbosity nonetheless, but they'll surely be less than 6K as you said.
Since I don't know the actual structure of your URLs and of your web application, it's the only solution that comes to my mind. If you provide further information, such as how are shaped the regular urls of your app, then I can update my answer
/end edit
This is not much a pratical solution, because it will require a lot of code, but if you want a design like that it's the only way that comes to my mind.
Also, consider you can use regexes as the $route index, but I don't think it can work here, as your usernames are unlikely matchable in this fashion, but I just wanted to point out the possibility.
OR
2) You can change your design pattern slightly, and assign another route to usernames, something along the line of
$route['user/(:any)'] = "polica/ogled/$1";
This will generate quite pretty (and semantic) URLs nonetheless, and will avoid all the hassle of escaping the other routes.
view this:
http://www.web-and-development.com/codeigniter-minimize-url-and-remove-index-php/
which includes remove index.php/remove 1st url segment/remove 2st url sigment/routing automatically.it will very helpful for you.
I was struggling with this same problem very recently. I created something that worked for me this way:
Define a "redirect" controller with a remap method. This will allow you to gather the requests sent to the contoller with any proceeding variable string into one function. So if a request is made to http://yoursite/jeff/ or http://yoursite/jamie it won't hit those methods but instead hit http://yoursite/ remap function. (even if those methods/names don't exist and even if you have an index function, it supersedes it). In the _Remap method you could define a conditional switch which then works with the rest of your code re-directing the user any way you want.
You should then define this re-direct controller as the default one and set up your routes like so:
$route['(.*)'] = "redirect/index/$1";
$route['default_controller'] = "redirect";
This is at first a bit of a problem because this will basically force everything to be re-directed to this controller no matter what and ultimately through this _remap switch.
But what you could do is define the rules/routes that you don't want to abide to this condition above those route statements.
i.e
$route['myroute'] = "myroute";
$route['(.*)'] = "redirect/index/$1";
$route['default_controller'] = "redirect";
I found that this produces a nice system where I can have as many variable users as are defined where I'm able to redirect them easily based on what they stand for through one controller.
Another way would be declaring an array with your intenal controllers and redirect everything else to the user controller like this in your routes.php file from codeigniter:
$controllers=array('admin', 'user', 'blog', 'api');
if(array_search($this->uri->segment(1), $controllers)){
$route['.*'] = "polica/ogled/$1";
}

Route all actions of a controller except one in codeigniter

I am doing a project in CodeIgniter and I want to route all the urls of a particular controller to a specific action except one. For e.g.,
I want the url
myurl/mycontroller/myaction
to be handled by the action myaction but any other urls like
myurl/mycontroller/myaction1
myurl/mycontroller/myaction2
myurl/mycontroller/myaction3
to be handled by action abc of a particular controller. I had searched across the internet and what I get is how to handle all urls by a certain controller except some. The way to do it is
$route['^(?!admin|user|setup|pages).*'] = "user/view/$0";
Here all urls will be handled by user/view except those whose 2'nd part of the url is admin, user, setup or pages.
I think routes are applied in order, so how about adding a route for the "myaction" first before the other ones?
$route['myurl/mycontroller/myaction'] = "myurl/mycontroller/myaction";
$route['myurl/mycontroller/abc'] = "myurl/mycontroller/$1";
I believe this is the correct syntax
$route['myurl/mycontroler/myaction(:any)'] = "myurl/controller_a/action";
You can verify it here
EDIT
I read your comment and I made an adjustment. Give it a go and see if it fits.
EDIT 2
Well since you just want the exact word myaction unharmed then either use (:any) or (\d+) after the word so the rerouteing happens when a number is attached to the myaction word. I haven't actually tested it yet.

Resources