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.
Related
[![This is the error][1]][1]
This is the Controller.php but i think this is a built in when creating a laravel project.
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
I have make an AdminController in App\Http\Controllers folder with an index method and the Request it is working fine, i type in url bar http://localhost/brosta/public/index and i take the path "index" to my browser it's ok!
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class AdminController extends Controller
{
public function index(Request $request)
{
$path = $request->path();
print_r($path);
return view('index');
}
But when i make the controller AdminController in a subfolder like App\Http\Controllers\Admin the requested path is not working. How can make it to work?
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
class AdminController extends Controller
{
public function index(Request $request)
{
$path = $request->path();
print_r($path);
return view('index');
}
}
Ok! Now i have a different problem with the Request! With this way is working
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index(Request $request)
{
print_r($request->path());
}
}
But with this way is not working!
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
$request = new Request;
print_r($request->path());
}
}
Try adding this line in your second controller.
use App\Http\Controllers\Controller;
Since you are in a different namespace you need to add the correct namespace for the Controller class.
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
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
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.