Handling multiple views controllers in CodeIgniter 4 - codeigniter

I am new to CodeIgniter 4.
I have one controller named Pages and helps to display website pages normally. Then I have another controller named Admin which is suppose to load the admin view with its own header and footer files.
While Pages controller works fine, the Admin controller keeps saying 404-file not found admin.
I think I have a problem with my current Route settings below. Please help.
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('default');
// Route Definitions
$routes->get('/', 'Pages::index');
$routes->get('(:any)', 'Pages::default/$1');

try ajusting your code like this.
$routes->setDefaultController('Pages');
$routes->setDefaultMethod('default');
// Route Definitions
$routes->resource('pages');
$routes->get('/', 'Pages::index');
$routes->get('(:any)', 'Pages::default/$1');

Related

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`

how to make sub folder for login page and rest of the pages in codeigniter

I am working in a codeigniter project in wamp server.
My current login page is http://localhost/flowers/login and its working correctly (no issue). The rest of the urls are like this
http://localhost/handycheck/admin/dashboard etc
My issue is i need to change the login url like this
http://localhost/flowers/admin/login
&
http://localhost/flowers/providers/login
Its because I have to maintain login form for multiple users.
How can i make this.
Please help me and thanks in advance who helps me alot..
You can do this by adding custom roue in codeigniter routing configuration as follows go to config/routes and add the following entry in this file
$route['flowers/providers/login'] = 'flowers/login';
$route['flowers/admin/login'] = 'flowers/login';
this will redirect the request to the login in flowers controller and if you need to do custom handling for admins and provider you can get the url segments and do custom handling according to user type
I hope my answer would be useful

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

how to get the admin login page by inserting /ci-admin like /wp-admin in wordpress

I am wondering if that is possible to view the admin login page just by typing /CI_ADMIN after the domain name just like we do in wordpress.
http://www.abc.com/wp-admin
and I want to do
http://www.abc.com/ci-admin
for my codeigniter website. is that possible ? the url thing
you can do this with routes just define route in you routes.php file
$route['ci-admin'] = 'ci_admin/login';
for more how to create admin panel please read this article specially third method
CI Admin panel

MVC3 - How do I route to a subsite?

I have a subsite within my main site for site Administration. The site is physically stored in the form
~/Views/Administration/ViewName/Index
With controllers inside
~/Controllers/Admin/ControllerName
I am getting an exception trying to visit the page.
The view 'index' or its master was not found or no view engine
supports the searched locations. The following locations were
searched: ~/Views/ViewName/index.aspx ~/Views/ViewName/index.ascx
~/Views/Shared/index.aspx ~/Views/Shared/index.ascx
~/Views/ViewName/index.cshtml ~/Views/ViewName/index.vbhtml
~/Views/Shared/index.cshtml ~/Views/Shared/index.vbhtml
I added a route
routes.MapRoute(
"Administration", // Route name
"Administration/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administration", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Going directly to the page manually
http://localhost:999/Administration/BaseItem/index
Does not result in exception, but I get no content. This leads me to believe it is not finding the View. What am I doing wrong?
I think the issue is that I have told the route engine how to route to the Controller, but I have not told the system how to route to the View. Where do I tell the system where the views are?
The problem is not with the routes, but with the design. The view engine cannot find your view, because it cannot find the correct path, since the default view engines are not designed to search for a subsite.
Instead of creating a subsite, make Administration an Area in your project. In AdministrationAreaRegistration.cs, you will set a route similar to the route you added. Place your views in the Views folder inside the Administration folder (inside the Area folder), and everything will work properly.

Resources