Method name as Route - codeigniter - 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.

Related

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

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 Routes Conflict

I am using codeigniter to create a restapi and I am having some problems with routes.
Here is the equation:
I have to navigate /users/ to index function,
I have to navigate /users/{MongoId} to /users/show/{MongoId},
I have to navigate /users/function to /users/function.
And here, my routes:
$route['api/users/do_login'] = "api/users/do_login";
$route['api/users/(.*)'] = "api/users/show/$1";
When I remove the (.*) routing (or both of them), my do_login function works successfully. But not my api-index function because Codeigniter takes MongoId as function name and fails.
When I write it back (or both), my index function works successfully, but my login doesn't.
Because it tries to send function name to show function as parameter.
Can you please help me to fix that?
Reverse the order of routes, CodeIgniters routes are prioritized.
You seek this structure:
$route['api/users/(:any)'] = "api/users/show/$1";
$route['api/users/do_login'] = "api/users/do_login";
also use (:any) instead of (.*) they are the same.
CodeIgniter Routing
Here, the working routes.
$route['api/users/do_login/(:any)'] = "api/users/do_login/$1";
$route['api/users/(:any)'] = "api/users/show/$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

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