Not displaying the controller in the URL - codeigniter

How can you hide the controller in the URL? When a non-registered user comes to my website, I would like them to see:
http://www.site.com/
Once they log-in, then they would be directed to the "Home" controller, then appearing as normal (i.e. http://www.site.com/home)
Any suggestions?

You can set the 'Home' controller as default, by using CodeIgniter's routing feature.
To set a default controller, open 'application/config/routes.php', and set:
$route['default_controller'] = 'Home';
CodeIgniter - Setting a default controller
CodeIgniter - URI Routing

Related

How can I redirect the home to a different php page with codeigniter?

When I enter the url mysite/home it goes to a php page called register.php. Where can I specify that it goes to login.php instead? Thanks
In application/config/routes.php look for $route['default_controller']
Set its value to the desired controller name you wish to used as default (only controller, no method). By default, users that go to your base url will be shown whatever is at name_of_controller_you_chose/index

Encrypt URL with PHP Codeigniter

I have developed a website with PHP Codeigniter. It consists of many controllers and views. I want to encrypt the URL of the site to hide the controller and function name. Currently, the URL looks like this :
www.sitename.com/controller_name/function_name
I don't want the users to see the controller and function names. Is it possible to do this now because the site is almost complete. Is there any shortcut of doing this? Please Help
You can use codeigniter URI Routing to remove your controller name from URL.
$route['url_name'] = 'controller_name/function_name';
This `www.sitename.com/url_name` will look go to `www.sitename.com/controller_name/function_name`
$route['url_name/(:any)'] = 'controller_name/$1';
This will only change your controller name.. `controller_name` to `url_name`

Routing problems, CodeIgniter

This might be a very silly question because I'm new to PHP frameworks. But I have got the concept of MVC framework. But this routing thing is very confusing me.
I created a controller and view for dashboard and set it as default. Then I wanted to check if the user is logged in then send him to the login page if it's not.
So, what I did in the routes is this:
$route['default_controller'] = 'dashboard/index';
$route['login'] = 'login';
But even if I add a thousand more controllers and routes, it still goes to the default i.e the dashboard. This was supposed to work http://localhost/codeigniter/login. I'm very sure I haven't put a redirect-to-dashboard code in the login page.
at first it's important to understand to routing in codeigniter.
The key for understanding is $route['url/path'] = controller/method (or "public function")
So. In your case, you routing to Login controller and index method (or index function...this it interpretation of codeigniter )
If you want to check if is user logged or not, you may try to check for examle session variable inside of called functon. But it depends on your application. But I think that is not good idea to check it in routes.php
Hope it helps you.
The default controller in routes, will be the controller that opens your index or first page.
If your first page is in controller "pages", then that is your default controller. Do not use it for your secure dashboard pages
Your first page should be the index method in the controller. It should look like this
$route['default_controller'] = "pages";
Where pages is your first page controller

301 Redirect Default Route in Codeigniter

I am trying to set up a 301 redirect for the "Default-route" in codeigniter.
I know I can set up a route to another page, but I need it to redirect instead.
Does anyone know how?
Yes you can set a controller function on error page where we will call a view
$route['404_override'] = 'errors/index_404';
errors is controller and index_404 is action(function) where we calling our custom view page

codeigniter routes

How to redirect controllers if they not match pattern.
For example I have follow controllers about_us, contacts, find_us. How to do so If the request method is not in that selection to be redirected to other controller ?
For some reason it's not in the documentation for URI Routing in the User Guide. If you are using CI2, inside of your routes.php file you can use "404_override". It is one of the reserved routes.
$route['404_override'] = "error";
So when a user comes to a controller that is not one you have created or not something inside your routes.php file, it throws them into the "error" controller. Obviously you would have to make a controller called "error"
Read about routing.

Resources