Folder Structure in Codeigniter HMVC Extension - codeigniter

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

Related

HMVC With Multilevel Hierarchy Codeigniter

I am implementing HMVC in codeignter3. I have added all the required file in core and third_party folder.
I have also created modules folder. Inside module I have created frontend folder Inside frontend folder I have created One folder test inside that I have created 3 Folder controllers, models and view. In controllers folder I have created Test.php and in models model created with name Test_model.php and in views one file created index.php
My controller code is
<?php
class Test extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('test_model');
}
public function index()
{
$data['main_content'] = 'home/index';
$this->load->view('front/layout', $data);
}
}
?>
inside application/config/routes.php
$route['test'] = "frontend/test/Test/index";
When I am accessing it through localhost/myprojectfoldername/test I am getting 404
yes, this gives you an error. because you have created modules folder inside the module folder.
you have to create modules folder in your application folder not to the inside of another folder the path is defined in mx_loader application>modules so the code is not fined the class.
change its directory and refresh it so the error will remove.
further help uses these links:
http://www.dcaulfield.com/how-to-install-codeigniter-hmvc/
Or you can directly download from my gmail drive :
https://drive.google.com/open?id=1Viyo7CQcjJNkBv5ahyiOs5RwwXiOVopg
afer downlad and set up, hit url:
http://localhost/hmvc/home
Use like this:
$route['some-route'] = "yourcontroller/yourmethod";
or in other words:
$route['user'] = 'user/login';
One thing about HMVC Pattern in Codeigniter is, you need to follow the folder structure very well.
In your description you said you have created a Test.php file in your controller which resides in a folder called "frontend" which is in another folder "test" in your modules folder too.
Now the trick here is, If your controller name (Test.php) is equal to the folder name (test), you can simply call it like this:
$route['test'] = "frontend/test/index";
Instead of this:
$route['test'] = "frontend/test/Test/index";
On the other side let's say you have created another file( or controller) in the text folder called User.php then you can set your route as:
$route['test/user'] = "frontend/test/user/index";
Then again in your route code example I noticed you tried to use the Uppercase for the "Test", it does not really matter you can just use a lowercase "test" instead:
$route['test'] = "frontend/test/test/index"; But note this was just to explain whether it is case-sensitive or not.
Try your hands and it and let's see the outcome

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

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/

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.

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.

Separate 3rd party modules

I going to start new Codeigniter application and trying to find a way to separate 3rd party modules source (for example Tank Auth) from my own code,
What i need is to setup a file tree like the following:
/project_root/
/system (framework system - done)
/3rd_party/
/dists/
/application/ (installed 3d party libraries/modules - the question topic)
/application/ ( my application - done)
/site/ (site root, working tree - done)
The /system /application /site done using index.php and application/config/config.php settings.
Is there any correct way to acheive the tree like above using configuration settings ? I new for Codeigniter and have no idea if such is even possible.
For a modular structure with Codeigniter, Modular Extensions - HMVC is the go-to solution:
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
For your setup, you would add this to your config.php:
$config['modules_locations'] = array(
// absolute path relative path
APPPATH.'3rd_party/application/' => '../../3rd_party/application/',
);
/project_root/
/system
/3rd_party/
/dists/
/application/
tank_auth
my_module
some_other_third_party_code
/application/
/site/
The "relative path" is relative to any sub directory in your application root, like models, controllers, helpers, etc. For example in CI you can load a "model" from a "view" directory if you use:
$this->load->model('../views/some_model');
...not that's it's something you would normally do - but this is how HMVC's loader works. HVMC can be used to load anything CI normally can but from a "module" directory. You can have as many different modules or module paths as you want.
Modules can (but don't have to) load controller and act like their own mini-application. You can also load dependencies cross-module, or from the default application root.
You can still use your default application directory alongside modules. To specify that you want to load a particular asset from a module path, just include the module name in the path:
// Loads the "tank_auth" library (works from the tank_auth module)
$this->load->library('tank_auth');
// Loads the "tank_auth" library (works from anywhere in your application)
$this->load->library('tank_auth/tank_auth');
This works with models, helpers, etc. as well.
To access a controller in tank_auth/controllers/login you would use the URL http://example.com/tank_auth/login. You can also run a controller within another controller by using the modules::run() method, which could allow you to "wrap" controller methods of modules in your own logic without touching them:
class my_login extends MX_Controller {
function index() {
modules::run('tank_auth/login');
}
}
It's pretty well documented and has been around for years. I have used HMVC in nearly every Codeigniter project I've ever done, I highly recommend it.
The problem is that loader class , that loads the library with $this->load->library("path/to/module") is using hard-coded "library" as folder name (check the source of Loader.php) . So, in other words all your modules and libraries have to reside in library folder
/project_root/
/system (framework system - done)
/library/
/3rd_party/
/application/
and load them then with $this->load->library('3rd_party/application/tankh_auth.php)`
The only way to correct this (and not using library folder) is by overriding Loader class by writing your own. create Loader.php in your application folder , in specific - application/core by overriding library method
class My_Loader extends CI_Loader {
function __construct() {
parent::__construct();
}
public function library($library = '', $params = NULL, $object_name = NULL){
//override this method with your custom loader code
//eg
include_once($filepath."/".$library); //where your $filepath is path to your library,
$this->_ci_loaded_files[] = $filepath;
return $this->_ci_init_class($class, '', $params, $object_name);
//again look at original methods and/or write your own by referencing those
}
}
You don't need to worry yourself with HMVC (Although it is great), and you don't need MY_Loader either.
I would read through http://ellislab.com/codeigniter/user-guide/libraries/loader.html - Namely the section on Application "Packages"
In here, you will see a method called
$this->load->add_package_path()
In which you can specify additional paths for the loader to look for you libraries.

Resources