How to use multiple controllers in CodeIgniter? - 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

Related

Codeigniter - retrieve parameter without controller name in URL

I'm trying to retrieve string as parameter using following URL scheme:
www.myapp.com/[String]
Is there any way I can do this?
*Based on my research, Codeigniter doesn't accept string parameters unless I include the Controller's name in URL: www.myapp.com/[Controller Name]/[String]
But this doesn't solve my problem :(
You're right, CI requires controller name at URI, but You can use default_controller.
At config/routes.php add route rule $routes['(:any)'] = 'welcome/index';, remove index.php from Your URL (there're many tutorials and how-to for this), and at last useuriclass at Yourindex()method ofwelcome` controller:
function index(){
var_dump($this->uri->uri_string());
}

user profile routes configuration in 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.

Method name as Route - codeigniter

How can I do the following in routes dynamically?
$route['notifications'] = 'admin/notifications';
$route['categories'] = 'admin/categories';
This means that any method name under my controller is the landing page.
I don't want the admin controller to appear in the url.
I would simply use $CI->router->method in routes by I can't use get_instance in routes config.
What do I have to do?
Thanks!
It's difficult to get dynamic routes since at that point there's not much of CodeIgniter loaded.
I use the following to move all the methods of a controller to the first segment:
$route['(?!(api|account|more))(\w+)/(.*?)'] = "admin/$2/$3";
$route['(?!(api|account|more))(\w+)'] = "admin/$2";
Where api|account|more are routes being ignored.

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

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