Laravel 5 - constantly repeating use statements - php

Is there a central place where I can put 'use' statements so I don't have to keep doing things like this with every single controller I create?
<?php namespace App/Http/Controllers
use Session;
use Auth;
use Input;
use Log;
use Carbon;
use Response;
use Illuminate\Routing\Controller;
class BlaBlaController extends Controller {}
Just seems to violate DRYness and seems inefficient.

Short answer: No.
The 'use' statements are resolving the namespaces for that file, so you can't inherit them from other files. It doesn't violate DRY because there isn't actually any logic that is being repeated.
Now if you don't want to have to include those use statement in every controller then you can just resolve the facade out of the global scope whenever you use it. For example the following will work from any namespace, without needing a use statement.
\Input::all();
In my opinion it looks a little cleaner to just include the use statement, but either will work.

Related

"use" statements in php file

I am a beginner in Laravel and while learning about "namespace" and "use" statements, I found that, for example, in Controllers, when we first write "use" statements it should be repeated inside of the function.
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Why is this working so? Why is not it enough to write "use" statements once without repeating in a function? And also, if I will create another Controller with the same namespace, should I write the same "use" statements there as well?
There are two different use in PHP:
To alias namespaced names,
to apply traits to classes.
The use at the top of the file aliases namespaced names into shorter local ones. Literally their only use is so you can write DispatchesJobs inside this one file instead of having to always use the fully qualified name \Illuminate\Foundation\Bus\DispatchesJobs.
use inside a class applies that trait to the class.
In this case you could omit the first use to alias the trait, and apply it using its fully qualified name:
namespace App\Http\Controllers;
class Controller extends \Illuminate\Routing\Controller {
use \Illuminate\Foundation\Bus\DispatchesJobs;
...
}
This does exactly the same thing, but is obviously rather verbose. Establishing a few aliases at the top of the file allows your following code to be terser.
Using keyword use outside class is just importing specific part called trait.
And using keyword use inside class is actually inheriting or implements to use that trait

Importing class in laravel 5.2

use App\User;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
do i need to write the above import class every time when i create a new controller?In laravel 4.2 it does automatically
is any other way to do this?
Yes, you do. Your app is under the namespace App;, so if you want to use the Input class, but you don't say use Illuminate\Support\Facades\Input, PHP will try looking for it under your namespace App; and an exception will be thrown since the class you're looking for probably won't be there.
Those are not a mandatory class.
When ever you are using those facedes you have to add
There is one way i know.
class something like CustomController
Add the all the common classed in that controller.
Now for every controller you can extend the new CustomController

Differences between Request classes

Laravel 5.1 has the following classes that seems to share the same name and some have similar behavior.
use App\Http\Requests\Request;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Input;
What are the differences between the Request classes and when should we use each class?
If you look at http://laravel.com/docs/5.1/facades you see that both the Input facade as the Request facade are facades of the class:
Illuminate\Http\Request
The App\Http\Requests\Request you're talking about is the same class.
There is one minor difference in the facades for Request and Input. See this post about the exact difference https://stackoverflow.com/a/29961400/1129489
Here is the tl;dr from the answer at https://stackoverflow.com/a/29961400/1129489
Don't use the Input class. It's the same as Facases\Request and is there for legacy reason.
As for my own code base I'm going to use the following convention:
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\Request;

how to use different namespaces in a controller in laravel 4.1

What I want to do is to use different namespaces in a controller. I have a tree scheme like this:
app/
app/controllers/
app/modules/
app/modules/modulename/
app/modules/modulename/controllers/
app/modules/modulename/controllers/modulecontroller.php
app/modules/modulename/models/
app/modules/modulename/models/modulemodel.php
What I want to do is to call a model from a controller in app/controllers/ folder. Therefore I am supposed to add namespace as follows:
use App\Modules\Facebook\Controllers\Facebook;
The problem is that when I add a namespace and use App::() function at the sametime, I get the following error:
Class 'App\Modules\Modulename\Controllers\App' not found
I think it is looking the App::() function in module folder. How can I solve this problem?
if you use App inside your App\Modules\Facebook\Controllers namespace, it will be interpreted as App\Modules\Facebook\Controllers\Facebook\App class.
since you don't want to have the previous namespace, you use a \ before App like:
\App::()
or put a use statement of top the class like use App;
You probably are creating an unusual namspace scheme. It appears you are namespacing every class from your module differently. You should namespace your code within your module only, like so:
// Adding Onur to the namespace prevents any future namespace collisions.
<?php namespace Onur\Facebook;
After creating your namespace you should add all classes that are outside of your namespace that you want to use as followed.
use Eloquent, Input, Validate, Etc;
This prevents you from adding a \ in front of every class instance, making your code hard maintain and prone to errors. It also gives you a good overview on all the classes you are using in the current class.
if you say
use App\Modules\Facebook\Controllers\Facebook;
then you are supposed to use Facebook instead of App... Or donĀ“t I understand your problem correctly?
if you say
use App\Modules\Facebook\Controllers\Facebook as FacebookController;
the you can use FacebookController in your file
if you need Access to the root App, you need to to root it using a leading \
\App::make()

performance and the use statment PHP, Laravel 4.x

Are there performance considerations when importing classes with the use statement in PHP and more specifically laravel 4.x ?
for example often times i have many use statements in my controllers as such:
use OrganisationController;
use Input;
use Redirect;
use Validator;
use View;
use Organisation;
use Sentry;
use User;
use Str;
use Lang;
use Application;
use Job;
use Upload;
class ApplicationController extends OrganisationController {
...
In my opinion, it should not effect on performance because, when you use a class that is not in the same namespace you use it with a preceding backslash like
\View::make(...);
This way you just telling that, the View class is within the global namespace, to make the code clean you can use use statement top of your class like
use \View, \Redirect, \Validator; // more
class WhatEver extends BaseController {
function index()
{
//...
return View::make(...);
}
}
So, in either way, php is loading (include/require) the class whenever it founds any use of the class but when it's not in the scope of current namespace you must provide the namespace and it could be directly from the code like
\View::make(...);
Or maybe using a use statement to keep your code clean but in both ways, php is loading the class during run time (if not already loaded) but you have to provide the right location (with namespace).

Categories