I am using HMVC strucutre in Code Igniter, where I need to load module model class insider base CI controller or helper how can I do this.
i.e: load faq model in side view_helper
folder strucutre
Thanks
you can load a model in your helper the same way as you can load it in your controller:
$this->load->model('Model_name');
Once loaded, you will access your model functions using an object with the same name as your class:
$this->load->model('Model_name');
$this->Model_name->function();
Related
I have a laravel project and frequently uses Facade with a usage of something like
\MyCustomFacade::renewalDate()
which point to a class /src/Classes/MyCustomFacade#renewalDate.
The goal is that VScode definition of the facade usage will point to the correct file class path.
try this package
its index all your facade model controller routes view you create and auto import your class by typing your facade
I am working with Laravel framework and I want to include my common classes in the controller file while I am creating controller from php artisan. For now it creates controller like
<?php namespace ReaPro\Http\Controllers\Home;
use ReaPro\Http\Requests;
use ReaPro\Http\Controllers\Controller;
class HomeController extends Controller {}
I want to include my common classes like
<?php namespace ReaPro\Http\Controllers\Home;
use ReaPro\Http\Requests;
use ReaPro\Http\Controllers\Controller;
use ReaPro\Model\Page;
use ReaPro\Helpers\Common;
class HomeController extends Controller {}
So how can I pre include my
use ReaPro\Model\Page;
use ReaPro\Helpers\Common;
Classes somewhere so that these may come with each controller by default when I create controller with php artisan ?
The Laravel GeneratorCommand class is quite easy to extend for your own needs. Here is the ControllerMakeCommand which extends GeneratorCommand. The most important part is in the getStub() method, from here you can see that it returns either controller.stub or controller.plain.stub depending on the options. These stubs are here.
In short:
Create your own command for generating controllers
Extend Illuminate\Console\GeneratorCommand
Implement your own command in the same way as Illuminate\Routing\Console\ControllerMakeCommand
Create your own stubs
In Laravel, when a request mapped to a specific controller method is made, does Laravel access/compiles all other controller files or just the file from involved controller?
Most of the classes you create in a Laravel application are loaded as needed. This includes Models, Controllers, Repositories... pretty much everything.
Autoloading is achieved using the PSR-4 spec. If you have a namespaced class like...
<?php namespace \Foo\Bar;
class Baz {}
Then it should live in...
app/Foo/Bar/Baz.php
When this class is used for the first time, the framework will attempt to load the class from that location.
In symfony2 every user created controller extends Controller Class as shown below,
class MyController extends Controller {
thus functions related to session handling are available with $this object, But controllers in Vendor and Core don't extend Controller class thus don't provide access to session related functions. So is there any way to use these functions without extending Controller class.
Presently I am using $_SESSION[], for setting and getting session variables.
Is there any way other than above.
Symfony2 provides a service for sessions, this is what you're trying to retrieve. All services in symfony2 are retrieved using the service container, which is what you're referring to with
$this->get('session');
To properly make use of the service container in your own controllers you can either...
Configure your controllers as services (see: here)
Extend the base Controller class provided by the Symfony2 stack (making the get() method available to your child Controller)
The first option is the correct way to go, you have full control over what services are then injected into your respective controllers (see service container documentation)
I am using hooks in my CI application "pre_controller hook specifically".
But the problem is Hooks are activated each time a request is issued to any other controller even controllers that i don't want the hook to be activated in.
Can hooks be enabled for only one controller? just like the #Before annotations in playframework.
Thanks in advance.
Why don't you put that logic in the Constructor of your controller?
If you have multiple controllers you wish to share this functionality, simply extend the CI_Controller with a new class in application/core/MY_Controller.php and put the functionality in there, then in the controllers have them extend that class instead of the default CI_Controller (you can add more than one class in MY_Controller.php)!
Anything that you put in the override class in MY_Controller.php would execute before the code in the rest of the controller, simulating the pre_controller hook.
Just remember to call the parent constructor as well:
function __construct(){
parent::__construct();
}
See the manual for more info about extending the core: https://www.codeigniter.com/user_guide/general/core_classes.html
You could also put your code into a library to use whenever you need it. I ended up using my solution because I could keep my authentication logic separate to my modules. It makes it easier for updating as well.