Codeigniter - Hooks per one controller - codeigniter

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.

Related

Create controller with already included classes laravel

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

ASP.NET Web API Help Pages Omit Controllers That Inherit From A Base Controller

I have a controller that I want to generate documentation for using ASP.NET Web API Help Pages.
When I directly inherit from ApiController the documentation appears:
public class ExampleController : ApiController
But when I inherit from a base controller, it is omitted:
public class ExampleController : ApiBaseController
...
public class ApiBaseController: ApiController
I have switched to delegation rather than inheritance, but I wanted to know how to make it work with inheritance.
Here is a tip I picked up in my experimentation.
The documentation leans heavily on the routes in your API config. If your controller isn't covered by a route, it won't show up. Additionally, the order of the routes in your API config is the order of the operations in your documentation.
To cover both of these points I have created named routes for each controller. This has the added benefit of making each route specific, rather than a single route with lots of optional bits. This ensures all my operations appear in the documentation, in a good order.
I have also added the API tester so the API can be called directly from the documentation.
Check the permissions in your base class. I had the same issue and is was a result of methods that should have been set as internal being protected.
Make sure that all your methods that need to be accessed by the parent item are set to internal and any methods that override the ApiController are set to protected.
Post your code if it still doesn't work.
Works like Gravy :)

Regarding Symfony2 Session

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)

Setting Session in MVC3 when the class not inherited from controller or helper

I would like to write and read data to Session even if my class not inherited from controller or helper. Something like this:
public class User
{
public void CreateSession()
{
Session["key"]=data;
}
public void ReadSession()
{
data=(string)Session["key"];
}
}
Important thing - I need to get instance of User class in some actions
and views. Am I be able to inherit User class from controller or
helper. Because I've already try that, but I got some errors.
How can I achive this?
Thanks in advance.
HttpContext.Session will work, it's a static available anywhere to an assembly that references System.Web.
If your code is in your main website assembly, this should work well. If it's not, you'll have to create a dependency on System.Web which you may or may not want to do.
You should do these kind of things in your controller.
You can access HttpContext directly, but that's not "the ASP.NET MVC way", and you loose some of the advantages of using MVC in the first place. Like making testing harder, and directly coupling your application to HttpContext...
Also, if it's a new session, or Session["key"] is not set for some reason, data=(string)Session["key"]; would throw a null reference exception.

Inject cookies into controller

Is there any way to inject the cookie dependecy to a controller? Or do i have to write my own interface and wrapper class around the Cookie collection class?
I think you're asking about whether you can get a Cookie as a parameter to an Action. I don't believe you can do this, so you'll have to hit the Cookie class directly.
What we do in this case (when cookie based data is required by most of the actions in an application) is put a utility method in a Controller base class and then have all our controllers descend from that. Makes it very easy to use the Cookie in an Action, and centralizes the code for extracting it.
Since no better answered surfaced I just implemented a interface and injected that for concrete scenario and Mocked it for test

Resources