Codeigniter model access from a file which is located at root - codeigniter

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.

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 change the symbolic link in development env shared hosting laravel?

I have a Larevel-app in a shared hosting. For the setup, I had to create a new folder in the main carpet of the hosting and copy the content of my public folder to public_html. I made changes in index.php and all working fine and nice. However, when a user upload a file that needs to be public, this file it saved in the myproject/storage/app/public path but not reflected in the public_html/storage so I can't access to it.
Reading the documentation, I know it is a problem with the symbolic link.
how can I change it?
Note: I can't access to the cdm because it is a shared hosting without access. It is window hosting.
make this route in web.php then hit this
Route::get('/artisan/storage', function() {
$command = 'storage:link';
$result = Artisan::call($command);
return Artisan::output();
})

Laravel - Change cookies directory from root to other

I have an application on Laravel 4.2. Currently, when I see the http requests sent from my application, the cookies are all stored in the root directory as path=/. I would like to store the cookies in a custom directory like another folder inside the storage directory.
Go to config/session.php,
set
'path' => other directory path (route path)

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.

Resources