Greedy Pointer
Greedy Pointer

Reputation: 344

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?

Upvotes: 0

Views: 576

Answers (2)

DFriend
DFriend

Reputation: 8964

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 {

Upvotes: 1

Sherif Salah
Sherif Salah

Reputation: 2153

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

Upvotes: 0

Related Questions