Type hinting parent::__construct() arguments in controllers - laravel

I've got a BaseController in a Laravel Framework based App with the following code:
class BaseController extends Controller {
public function __construct(Credentials $credentials) {
$this->credentials = $credentials;
}
Then, all my other controllers will Extend the BaseController:
class PostController extends BaseController {
public function __construct(PostRepository $post)
{
$this->post = $post;
parent::__construct();
}
However, I'd need to type hint the Credentials Class in the parent::__construct(); of all my controllers. Is there any way to avoid that?
Thanks in advance

I can solve it using the following code:
class BaseController extends Controller {
public function __construct()
{
$this->credentials = App::make('Credentials'); // make sure to use the fully qualified namespace of Credentials if need be
}
}

Related

Laravel Controller: Override parent request validation

I have a structure that has base controller called BaseController that all controllers extend it, and it has implementation of index() to view resources, base controller use default Request validation and I need to override this in a child.
When I run the below code I receive the following error:
Declaration of ...PostController::index(...PostRequest $request) should be compatible with ...BaseController::index(...Request $request)
Parent:
/* BaseController.php */
namespace App\Http\Controllers;
use Illuminate\Http\Request;
abstract class BaseController extends Controller
{
public function index(Request $request)
{
// logic
}
}
Child:
/* PostController.php */
namespace App\Http\Controllers\App;
use Illuminate\Http\PostRequest;
class PostController extends BaseController
{
public function index(PostRequest $request)
{
// logic
}
}

Laravel extends class with constructor using DI

I have created a small CRUD controller, I need to inject FormBuilder in the __construct() of this CRUD controller
class CrudController extends Controller
{
private $formBuilder;
/**
* CrudController constructor.
* #param FormBuilder $formBuilder
*/
public function __construct(FormBuilder $formBuilder)
{
$this->middleware('auth');
$this->formBuilder = $formBuilder;
// ....
My question is : can I extends this class without injecting FormBuilder to each child ? For now I have :
class LevelsCrudController extends CrudController
{
public function __construct(FormBuilder $formBuilder)
{
parent::__construct($formBuilder);
// .....
Which is really redondant, any workaround ?
If you do not want to have a FormBuilder $formBuilder in your child controller's construct, you could pass it as a param only when you call the parent's construct like this:
parent::__construct(resolve(FormBuilder::class));
Otherwise, you could remove it completely from the parent's constructor and resolve it inside the parent constructor like this:
class CrudController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->formBuilder = resolve(FormBuilder::class);
// ...
}
}
And then in your child, you can simple call:
public function __construct()
{
parent::__construct();
// ...
}

codeigniter seems not reusing model object

Typically, codeigniter uses below structrue.
class AController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('ModelA');
$this->load->model('ModelB');
$this->load->model('ModelC');
...
}
}
and I'm very new in codeigniter, I'm just wondering If I can make this more fast. Should I load these models whenever being made each request? It causes slow response.
In case of python tornado, they can store database model object so that all request share one model object. Is there any method like this in codeigniter?
You need to make sure your model is loaded before using it.
1.You can write it inside Controllers construct function so that you don't need to load the model inside other method.As example
class AController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('ModelA');
$this->load->model('ModelB');
$this->load->model('ModelC');
}
public function index()
{
$this->ModelA->model_funtion();//now you can call model function for m any function of controller.
}
}
2.You can call it before using it.
class AController extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->model('ModelA');
$this->ModelA->model_funtion();
}
}
3.You can load inside autoload.php.So that model can be used everywhere(inside any controller).
If you want it in most places, but not in all places, then as an alternative, you can create a Custom Controller and then Use it.
1. Code for /application/core/Custom_Controller.php :
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Custom_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('ModelA');
$this->load->model('ModelB');
$this->load->model('ModelC');
}
}
2. Then Extend the Controller as :
include_once(APPPATH.'core/Custom_Controller.php');
class AController extends Custom_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
// Do something
}
}
Please try it..
class AController extends CI_Controller
{
function __construct(){
parent::__construct();
$this->load->model('ModelA');
$this->load->model('ModelB');
$this->load->model('ModelC');
}
public functionname()
{
$this->ModelA->modelfunctionname();
}
}
I realized the reusing is not possible, But I found better way.
something like this.
class AController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$method = $this->router->fetch_method();
switch($method) {
case "method1":
$this->load->model('ModelA');
case "method2":
$this->load->model('ModelB');
break;
default:
$this->load->model('ModelA');
$this->load->model('ModelC');
...
}
}

How to inject repository pattern to event Laravel

I use repository patter. If I want use repistory in my controller(for example UserRepository) I simple inject repository in my controller
class SomeController extend BaseController
public function __contruct(UserRepositoryInterface $user){
$this->user = $user;
}
In this example laravel automaticlly inject repository to my controller. But now I would like make some operation on my database from event. So I would like inject UserRepository to my event. How can I do it?
class UserHandlerEvent {
public function onCreate($event){}
public function subscribe($events){
$event->listen('user.create', 'UserHandlerEvent#onCreate');
}
This is my event, next I regiter it in my EventServiceProvider. It is looks like
class EventServiceProvider extends ServiceProvider
{
public function register(){
$this->app->events->subscribe(new UserEventHandler());
}
}
What should I do, if I want have to access to UserRepository from my event?
Just create a constructor in your UserHandlerEvent class and inject the UserRepositoryInterface as you did in your controller.
class UserHandlerEvent {
protected $user;
public function __contruct(UserRepositoryInterface $user){
$this->user = $user;
}
public function onCreate($event){}
public function subscribe($events){
$event->listen('user.create', 'UserHandlerEvent#onCreate');
}

CodeIgniter constructor is not work proper

class model_demo extends CI_Model{
function model_demo()
{
parent::Model();
}
}
I'm working with CodeIgniter with a constructor, and it's producing a fatal error.
class model_demo extends CI_Model{
function model_demo()
{
parent::__construct();
}
If you are using Codeigniter 2 or abore, try:
class model_demo extends CI_Model
{
function __construct()
{
parent::__construct();
}
}
You can see the syntax here:
Models: Codeigniter User guide

Resources