HMVC With Multilevel Hierarchy Codeigniter - 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

Related

Laravel Route to Standalone WebApp

I am trying to build a portal in Laravel to serve some other, standalone web apps (not built in Laravel), but I am struggling to find out how to route to these apps if I want to place them outside the public folder.
In the past, I would use (temporary) symlinks for this kind of things, but I was wondering if Laravel provides another solution.
So, I have a folder:
module
module/index.php
module/js/whatever
module/css/whatever
module/img/whatever
and I want a route /modules/1 to link to index.php in the module-folder in such a way that the resources in this folder (js/css/img) are also accessible.
Any suggestions?
You can include other PHP files with require_once.
web.php
Route::any('/webapp/{assets?}', 'WebAppController#index');
WebAppController
class WebAppController {
public function index(Request $request) {
require_once '../module/index.php';
if ($request->assets) {
// check from session if user is logged in
// require asset as well
// (or download them https://laravel.com/docs/5.5/responses#file-downloads)
}
}
}
http://php.net/manual/en/function.require-once.php

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.

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

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.

How to call my model or controller of codeigniter outside application directory?

I have the following doubts:
I have my project in codeigniter which uses Flash. Its structure is as follows:
name_proyect
application /
controller /
model /
view /
audiofiles/
//Here are being saved all audios.
user_guide /
flash /
archivos.swf
save_audio.php
system /
index.php
The flash uses save_audio.php file, which is saving an audio on my server.
However, when I save the physical file on my server also need to save the file path in my database associated with the user.
My question is, I can use the active record in the file of codeigniter save_audio.php? o I can use my model or controller that use the model in my project that saving to database?
NOTE: save_audio.php is a simple script (isn't a class)
Move the current content of the save_audio.php into a CI controller. If you can, edit the flash file so it sends the files/requests to the new place.
If you can't update the flash file, remove the save_audio.php so CI will pick up the request, and use the routes config to make the path routed to your new controller inside CI. In this case you will end up something like this:
application/controllers/upload.php - the newly created controller
class Upload extends CI_Controller {
public function handle_audiofile() {
// the original content of the save_audio.php comes here.
// might need to update some superglobal usage
}
}
application/config/routes.php
$route['application/flash/save_audio.php'] = 'upload/handle_audiofile';

Resources