Extending codeigniters controllers - model-view-controller

I have a controller called 'admin'
class Admin extends CI_Controller {
and under the admin controller I want to have a series of controllers like:
class Settings extends CI_Controller {
class Preferences extends CI_Controller {
Is it possible to have these controllers extend the Admin controller?
like
class Settings extends Admin {
Is it possible?

Yes, it is possible. You just have to create your extended controller in the /core/ directory of your application, like so:
class MY_Controller extends CI_Controller
{
...
}
Then, you can create controllers that extend MY_Controller in the /controllers/ directory of your application, like so:
class SomeClass extends MY_Controller
{
...
}
Pay attention to the class and file naming convention.
Read more about it in the CodeIgniter User Guide, on how to extend the Core.

Related

Extends REST_Controller and MY_Controller at once in CodeIgniter

I have this situation where I need to access a global function and normally I will do that in MY_Controller for the normal CodeIgniter. But in this case, I am using a REST server and I noticed that the normal controller must extends REST_Controller to make it functioning.
From my AuthController
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
class AuthController extends REST_Controller {}
REST_Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
abstract class REST_Controller extends CI_Controller {}
I want to use MY_Controller to create a global function that can be accessed by any controller. For example, from AuthController I want to extend it to MY_Controller, but by doing so I won't be able to use the REST_Controller.
Is there any way I can extend to MY_Controller and REST_Controller at once so that I can use both functions from these two controller?
From your Auth controller, extends MY_Controller, and in MY_Controller extends REST_Controller.
It will be like
AuthController
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class AuthController extends MY_Controller {}
MY_Controller
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class MY_Controller extends REST_Controller
You can write your global functions in your MY_Controller.

Laravel Assigning Middleware within Controller’s constructor, should the constructor include parent::__construct()?

Here is example for
Laravel: Assigning Middleware within Controller’s constructor
https://www.tutorialspoint.com/laravel/laravel_controllers.htm
class UserController extends Controller {
public function __construct(){
$this->middleware('auth');
}
}
My question is should it be
class UserController extends Controller {
public function __construct(){
$this->middleware('auth');
parent::__onstruct();
}
}
Due to the UserConstroller override the __construct. If it does not include parent::__construct(), the parent constructor will not be called. That could be a problem, correct?

Setting up Queue in Lumen Framework

I'm trying to set up queue in Lumen using the guide from lumen page:
http://lumen.laravel.com/docs/queues
<?php
namespace App\Jobs;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
class BlastEmail extends Job implements SelfHandling, ShouldQueue
{
public function sendEmail()
{
[...CODE TO SEND EMAIL...]
}
public function handle()
{
$this->sendEmail();
}
}
and in My Controller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Jobs\BlastEmail;
use App\Models\Blast;
use App\Models\Subscriber;
use Illuminate\Http\Request;
use Validator;
class BlastsController extends BaseController
{
public function queue(Request $request)
{
$job = (new BlastEmail($email,$request->input('content'),$request->input('title')));
$this->dispatch($job);
}
}
Controller.php
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController
{
//
}
BaseController.php
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
use Cartalyst\Sentinel\Native\Facades\Sentinel;
class BaseController extends Controller {
function someFunctionThatOtherGuyWrote()
{
// Some code that other guy wrote
}
}
And I got
Undefined method App\Http\Controllers\BlastsController::dispatch
Do I miss something?
Looking at your code, your BlastsController extends App\Http\Controllers\BaseController and not App\Http\Controllers\Controller.
You should change it to extend Controlller class changing class BlastsController extends BaseController into class BlastsController extends Controller because this class will finally uses Laravel\Lumen\Routing\DispatchesJobs trait that contains dispatch method
EDIT
After update you didn't show full BaseController file but it seems you extends wrong class. You extend Illuminate\Routing\Controller and you should extend App\Http\Controllers\Controller

Share a common parent path when implementing Spring controllers

I want to be able to define a parent Controller class which will have a mapping of "/api", and then extend that controller with my different implementations.
So ApiController will have:
#Controller
#RequestMapping("/api")
For example, my User controller should extend the base api controller and also add "/users" to the path, so it will answer to "/api/users" requests. So UserController will have:
#Controller
#RequestMapping("/users")
but since it extends ApiController, it will effectively answer to /api/users.
Naturally I can prepend "/api" to all controllers so that this is achieved without the parent class, but I prefer to do it "the right way" if it's possible, so that I can define my api implementations with a cleaner and more visible path.
I tried extending the ApiController base class, but this does not work, UserController still answers to "/users" and ignores the base class "/api".
Hmmm. You can try this, it is what works for me:
#RequestMapping("/abstract")
public abstract class AbstractController {
}
#Controller
public class ExtendedAbstractController extends AbstractController {
#RequestMapping("/another")
public String anotherTest() {
return "another";
}
}
Note, your base class must have no #Controller annotation and must be abstract.
If you try to do extend not abstract class annotated as controller and that has #RequestMappings you get errors on step where RequestMappingHandlerMapping initializing.
You can achieve it with:
create sub-context configuration for controllers with same path prefix
create sub-context with own DispatcherServlet mapped to certain path
Look at this answer. It's similar problem, but for #RestControllers.

Codeigniter Multi-level Inheritance

I am having problem in using multilevel inheritance as follows.
I have a top level controller that extends CI_Controller class
class Application extends CI_Controller
{
}
A controller named 'Site' and 'Admin' extends the Application controller as
class Site extends Application
{
}
class Admin extends Application
{
}
And finally class 'User' and 'Guest' extends 'Site' controller
class User extends Site
{
}
Class Guest extends Site
{
}
The problem is, in User and Guest controller I am not able to load core libraries such as pagination, form_validation etc. using
$this->load->library('pagination);
But it works when I load the library in Site controller or Application contoller ie. controller that extends the core CI_Controller and it's child controller. When I try to load in grand child it doesn't work.
Can somebody clarify why this is happening? Thanks...
I've not seen a multi-level constructor class set up before, but it should work.
Are you calling parent::__construct() in the constructor of each class?
Checkout CodeIgniter Base Controllers, explanation included.
//-Create MY_Controller.php on application/core/MY_Controller.php
//contents of MY_Controller.php
class Application extends CI_Controller{
function __construct(){
// Call the CI_Controller constructor
parent::__construct();
}
}
//that is all now you can inherit class (Application) anywhere in your project

Resources