How to point multiple controller url to same URL in codeigniter? - codeigniter

I am working on a back-end Admin Panel using codeigniter and in admin panel there are multiple user roles like Admin, Editor, Manager etc.
Now i have created multiple controller for each roles like:
controllers/Admin.php
controllers/Editor.php
controllers/Manager.php
So, URL become like this
www.mysite.com/admin/anyfunction
www.mysite.com/editor/anyfunction
www.mysite.com/manager/anyfunction
But i want these URLs like
www.mysite.com/u/anyfunction
www.mysite.com/u/anyfunction
www.mysite.com/u/anyfunction

In your scenario function names must be different. But having different function names you can just make routes as
$route['u/any-method'] = "admin/any_method";
$route['u/some-other-method'] = "editor/any_method";
URL need to be different to allow Router class to resolve which part of code should be executed.
Check here in docs.

you wanted to do like this routing ?
$route['u/(:any)'] = "admin/method";
$route['u/(:any)'] = "editor/method";

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

What is the best practice to handle friendly urls in Laravel?

We have an online-shop.
We can have any friendly urls
as for separate models like products or categories (/iphone-6-white = /iphones/23)
as for custom filter urls like (/cool-flashes = /flash?cap=32gb&col=white)
What is the best practice to handle all urls?
IMHO we should create a table where we should store two cols (urlFrom and RedirectUrl). But redirect is not good way for us because we do not want a redirect, we just want to show appropriate content under the urlFrom.
I want to store it in a single table to make just one request to DB to find out whether we have the url or not.
You can create submit your form in array such as search[name], search[price][form] and create your custom Middleware that will checks request contain of search[] build url and after that redirect to this page.
Of course after that you must create route for those page, like:
Route::get('/search/{searchName?}/{searchPriceFrom?}', 'ProductsController#search');
Where your RoutesServiceProvoider will look like this:
Route::bind('searchName', function($term) {
// your DB query and checks...
});

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

multiple default controllers in Codeigniter site

I Have a add multiple default controllers in Codeigniter site.
I am unable to do that.
Basically I have to remove controller names from url without .htaccess file.
I want to make that dynamic.
I have checked that only default controller name can be removed thats why I want to make dynamic multiple default controllers.
Anyone can help me on this issue.
Thanks
You will have to write the changes to the application/config/routes.php file to do this.
So, if you have a controller called Secondary and it had methods like view, add, search
you would add:
$route['view'] = 'secondary/view';
$route['add'] = 'secondary/add';
$route['search'] = 'secondary/search';
NB that if you're passing params to your methods you will need to add entries for these as will, for example if you wanted to pass something to view() you would need to:
$route['view'] = 'secondary/view';
$route['view/(:any)'] = 'secondary/view/$1';
Hope this helps!

CodeIgniter, Multiple url possibilities that direct to same method

I just started learning codeigniter, and i noticed that. After writing my own routes like this
$route['index'] = "front/index";
$route['page/(:any)'] = "front/page/$1";
$route['section/(:any)'] = "front/section/$1";
Now i can visit the methods and controllers using the old routes and the new ones.
URLS now possible:
localhost/index
localhost/front/index
localhost/front/index.html
Too many urls directing to the same location, i was wondering if it were possible to have only 1 url per each method, restricting all others without using external code. From code igniter itself.
Also: this destroys my ability to use the uri class to get segments from the url.
The easiest way is to just have your index page as your default controller's only (index) function, In your case just the
$route['default_controller'] = "front";
Then all other pages have other controllers with their corresponding URI names, then you dont need ANY routing except the default. I ususally end up with a handful controllers like page.php, news.php, blog.php, products.php, admin.php etcetera.

Resources