How to use seaparate controllers in folder modules on codeigniter HMVC? - codeigniter

CI HMVC tree defaults:
modules/
..login/controllers/Login.php
..home/controllers/Home.php
i want to create this tree
modules/
..frontend/login/controllers/Login.php
..frontend/home/controllers/Home.php
..backend/table/controllers/Table.php
..backend/password/controllers/Password.php
But when i try to call http://domain/frontend/login : this give me 401 error result.
How to do this?

the way to do this is
modules/
..frontend/controllers/login/Login.php
..frontend/controllers/home/Home.php
..backend/controllers/table/Table.php
..backend/controllers/password/Password.php
but be aware - if you want to access to login you need to call the folder and the controller even if they have the same name e.g. http://ci.dev/backend/login/login/

Related

Redirection issue in Codeigniter4

I have done admin controller and put that in a sub folder named 'Admin'
Controller
Admin
-login.php
Now I want to fetch that by router file where I wrote this
$routes->get('admin', 'Admin/Login::index');
But it is showing me "Not found" error and redirects to "http://localhost/admin".
Could there be some .htaccess issue?
replace this
$routes->get('admin', 'Admin/Login::index');
with
$routes->get('admin', 'Admin\Login::index');
also make sure you add namespace in your login.php
namespace App\Controllers\Admin;
If you keep CI4's directory structure intact you could in fact use sub-folders for Controllers, Models, Views, etc.
For example app/Controllers/Admin/Login.php is a valid place to put a Controller class. Make sure to add the appropriate namespace in Login.php - namespace App\Controllers\Admin; Also in routes - $routes->get('admin', 'App\Controllers\Admin\Login::index'); It is quite possible to work without the prefix of App\Controllers, but I never extensively tested it and I think there was a problem in some versions of CI4 before.
Another issue could be your app/Config/App.php class. If you did not change anything in your .htaccess file (the one in public directory!), $baseURL should be set to your public directory address - http://localhost/myproject/public/ . Or if you wish to make it easier - set up virtual hosts.
Just a thing to add - get() method in $routes allow only GET requests, meaning if you are trying to POST something (or use any other HTTP request method) it will fail and redirect.

Codeigniter model access from a file which is located at root

In CodeIgniter HMVC, I want to access a model of user modules from the root directory.Actually, i want to access database, model and all the things.
Just manage a config item in config.php.That is application/config/config.php add a config item like this..
$config['modules_locations'] = array(
'modules/' => '../../modules/' //referencing at your root having modules folder
);
Note: It is not necessary that your folder must named as modules .You can named it as my_modules etc..
Then create a folder modules in root folder and access every thing like you do in application folder.
Hope it works great.

Codeigniter Issue route issue

I want the following to happen
The url something.com/why-us to go to content controller
The url something.com/nepal to go to location controller
How to manage the above in CI routes
Apply the below coding in routes.php in config folder
$route['why-us'] = "content_controller";
$route['nepal'] = "location_controller";
use your desire controller

Routes for accessing controllers inside vendor folder of laravel

I have LfmController.php of tswaler laravel-filemanager package with folder structure such as project\vendor\tsawler\laravel-filemanager\src\controllers\LfmController.php.
I used route as Route::get('/laravel-filemanager', 'Tsawler\Laravelfilemanager\controllers\LfmController#show'); for accessing that controller's show() method.But it gives:
ReflectionException in Container.php line 737:
Class App\Http\Controllers\Tsawler\Laravelfilemanager\controllers\LfmController does not exist error.This worked in my previous project but it now doesn't work on current project.What should be route structure to access controller inside vendor folder,like above?How to debug this kind of issue?
You should add '\' in the beginning of the controller namespace:
Route::get('/laravel-filemanager', '\Tsawler\Laravelfilemanager\controllers\LfmController#show');
By default, routes.php assumes your controller is in 'App\Http\Controllers' namespace but adding '\' will cause it to look in the root namespace.
In my case I forgot to set 'use_package_routes' to false in the config/lfm.php to enable my custom routes.

Folder Structure in Codeigniter HMVC Extension

I new with hmvc and I want to ask about folder structure in codeigniter with hmvc extension.
I used to arrange application controller folder for admin and public like this:
Application
--Controllers/
----admin/
------login.php
------dashboard.php
----blog.php
----about.php
----contact.php
So that page could be access with {base_url}/admin/login, {base_url}/admin/dashboard for Admin and for Public is {base_url}/blog , {base_url}/about etc.
With hmvc extension, how to achive url like that? for now i make my filename with admin_something.php, but that is look messy for me.
Application/
--Modules/
----admin_dashboard/
------Controllers/
--------admin_dashboard.php
------Models/
------Views/
----home/
------Controllers/
--------home.php
------Models/
------Views/
Thank you
You can have multiple controllers in your module. So make a folder admin with admin controller in your module's controller folder.
mymodule
- controller
- home.php
- admin/admin_dashboard.php
- model
- model.php
- view
- home_view.php
- admin/admin_view.php
To access the modules
echo Modules::run('mymodule/home/methodname');
echo Modules::run('mymodule/admin/admin_dashboard/methodname');

Resources