user profile routes configuration in codeigniter - codeigniter

I have a simple problem but I just can't figure it out. I wrote a method that reads parameters from the url (one parameter which is the username) and queries all data to that user and displays it in the page.
the only problem is that the method is part of a controller and naturally it has to show in the url (which is simply said: not too nice for sharing. Also I have to write the string as follows:
www.domain.com/controller/profile_guest?user=username
I want to get rid of all that is before username. So for it to show as:
www.domain.com/username
Now there are two scenarios to cover here.
a) someone browse the catalogue, clicks on profile name and then redirects to profile page. once there he likes what he sees, he copies url, and share it.
b) someone receives the shared link and clicks on it, so what should load is the profile page with the url he received not the full one showing controller and method.
is this possible to achieve?
Thanks for the help :)
Update:
$route['default_controller'] = "main/index";
$route['404_override'] = '';

you can use this route:
$route['(:any)'] = "controller/profile_guest/$1";
but any other route, you will need to write it manually above this route.
for example:
$route['controller/view'] = "controller/view";
$route['(:any)'] = "controller/profile_guest/$1";
Explain:
The routes are handled in the order in which they appear on your routes.php file, so if you put $route['(:any)'] at the top, it will handle anything.
suppose that you have a contoller and a function inside it, and you need to execute it for example: www.domain.com/contoller/function, you cann't execute your function because it will match this route $route['(:any)'], so you need to define a route for it before $route['(:any)']
for example:
$route['contoller/function'] = "controller/function"
$route['(:any)'] = "controller/profile_guest/$1";
and do the same thing with all your contollers and its function, but you have to put $route['(:any)'] at the last route.

Related

How to use multiple controllers in CodeIgniter?

I need to use 2 controllers in CodeIgniter 3. I have Welcome and Paypal controllers. In routing, previously I had following code:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Then to add Paypal, I tried following codes, which were not helpful:
//$route['(:any)'] = 'paypal/index';
//$route['Paypal'] = 'paypal/index';
//$route['Welcome/Paypal/(:any)'] = 'welcome/index';
I tried them separately and also together but still I get this result:
404 Page Not Found
The page you requested was not found.
What I need to write, for using my Paypal controller as well?
If, for example, you want to go to the URL https://example.com/paypal you do not need a route if the Paypal controller has an index function.
If you wanted a way to "buy" a pair of socks that used the URL https://example.com/buy/socks but wanted to handle this request using the PayPal controller method buy($item), then you need a $route.
$route['buy/(:any)'] = 'paypal/buy/$1';
But you do not need a route if your "buy" URL is https://example.com/paypal/buy/socks
The only time you need to define a $route is when you want to deviate from CodeIgniter's controller/function[/arg1[/arg2[...]] URI pattern.
Your problems may not be route related. Be certain you followed the CodeIgniter rules for controller file and class naming? The name of the file must begin with an uppercase letter, i.e. Paypal.php and the class definition must match the file name exactly. i.e.
class Paypal extends CI_Controller {
What i got from your question is that you want the paypal controller to be rerouted or redirected to welcome controller.
if i got it right, you can simply do that with redirect .. in your paypal controller use redirect in your construct or index to redirect you to welcome controller

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";

Hide Codeigniter controller name from URL with multiple controllers

I'm using the following code in my routes.php to hide the controller name from the URL structure:
$route['(:any)'] = "auth/$1";
It works great, but my problem is this: When I want to access another controller, it seems to treat it as a function of the hidden controller.
So for example. I have
http://mysite.com/controller1/somefunction
which turns into:
http://mysite.com/somefunction
What if I want to access:
http://mysite.com/jsonfunction/anotherfunction/
How can I access the other controller while keeping the other one hidden? I really don't want visitors seeing http://mysite.com/maincontroller/ that's just redundant!
You are going to have to define your routes more specific I am afraid.
You can still use:
$route['(:any)'] = "auth/$1";
But it will probably go to the button of your list of routes.
if you want other routes to be added that overrule that one you'll have to place them on top.
For example like this:
$route['login'] = "auth/login";
$route['varY'] = "controllerX/varY";
$route['varY/(:any)'] = "controllerX/varY/$1";
$route['foobar'] = "controller/method";
$route['(:any)'] = "auth/$1";
See this document for more info and future reference:
http://codeigniter.com/user_guide/general/routing.html

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";
}

how to create Codeigniter route that doesn't override the other controller routes?

I've got a lot controller in my Codeigniter apps, ex: Signup, Profile, Main, etc..
Now I want to build "User" controller.
what I want:
if people goes to url: example.com/signup, I want use default route to "Signup" Controller
if people goes to url: example.com/bobby.ariffin, I want to reroute this to "User" Controller because the url not handled by any Controller in my apps.
I had create this in my config/routes.php:
$route['(:any)'] = "user";
but it's override all the route in my apps to "User" Controller.
Is there any simple route for Codeigniter that doesn't override the other controller routes?
Update---
I've got simple regex for this problem, from: Daniel Errante's Blog
$route['^(?!ezstore|ezsell|login).*'] = “home/$0″;
where ezstore, ezsell, and login are the name of controller in Your Apps.
You can also use a foreach statement for this. That way you can keep your controllers in a nice neat list.
$controller_list = array('auth','dashboard','login','50_other_controllers');
foreach($controller_list as $controller_name)
{
$route[$controller_item] = $controller_name;
}
$route['(:any)'] = "user/display/$1";
You're going to have to explicitly define all of those routes. Otherwise you will always end up at the "user_controller".
$route['signup'] = "signup";
$route['(:any)'] = "user/display/$1";
or something similar. They are ran in order, so what ever is defined first, is going to happen first. So if you catch (:any), you're going to send ANYTHING to that controller.
Also keep in mind that you can use regular expressions, so if you know there is always going to be a '.' in there, you could test for that.

Resources