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
Related
I'm working with laravel 5 and trying to integrate the following package:
exacttarget/fuel-sdk-php
I executed on my project:
composer require exacttarget/fuel-sdk-php
So I had on my vendor dir exacttarget provider.
First thing I've noticed this particular package doesn't use namespaces, so it still calls require directives but not "use \path\namespace"
Is it a right approach? I haven't seen many packages yet but among my past experience doesn't look to me the right approach to write a package...
After this I edit condif/app.php to use ET_Client class.
'providers' => [
...
'ET_Client',
...
],
Once I did this, I got an error: looks like Laravel frmwk tries to instantiate the class, that needs some parameters to work, even if I'm not yet using it (istantiating). It this a normal behavior from Laravel?
Am I missing something ?
The providers array is for registering service provider classes. Unless ET_Client extends Laravel’s base ServiceProvider class, it’s not going to work.
Instead, just add the use statements to your PHP classes as and when you need to use the class:
<?php
namespace App\Http\Controllers;
use ET_Client;
class SomeController extends Controller
{
public function someAction()
{
// Instantiate client class
$client = new ET_Client;
// Now do something with it...
}
}
I'm in developing of a new package in laravel5. At least my structure looks quite the same as here: https://github.com/cviebrock/laravel5-package-template
The problem occurs in the package controllers when I'm trying to extend from 'App\Http\Controllers\Controller' or set the alias of this class with 'use'. Here I'm always getting errors that the class "Controller" is not found.
I've setup my application in a different namingspace than "App", at my point MyCoolApp for example. So when I'm using 'MyCoolApp\Http\Controllers\Controller' it works, but that could not be the solution for an OpenSource package.
How can I reference to the the App-Controllers within the Namingspace without using 'App' or any other application-namingspace-string?
I don't know the reason why you want to extend App\Http\Controllers\Controller because it's specified for each Project and its behaviour could be changed because of setting in each Application.
I also take a look at App\Http\Controllers\Controller.
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;
}
I think you should extends Illuminate\Routing\Controller instead.
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 my laravel app, i have a controller that does the following before the definition of the controller:
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Payer;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Address;
Simply put, How can i make all the models in the API directory to be used?
Import the PayPal\Api namespace.
use PayPal\Api;
Then you can use the classes via Api\Class.
$payment = new Api\Payment;
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.