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;
Related
I can't make interfaces work in my php/laravel project. I have a series of Controllers, which I want to implement an interface. And PhpStorm seems happy with my syntax. But I get a runtime error that the interface is not found, when I make a check if the current object indeed has the interface:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use iTeamEntryController;
class TeamController extends Controller implements iTeamEntryController {...}
and then the Interface:
<?php
use Illuminate\Http\Request;
interface iTeamEntryController
{
public function saveTeam(Request $request);
}
Here I get an error on the line defining the class, saying that interface 'iTeamEntryController' is not found. Both the class and the interface exists in the same folder.
I've been trying to find an example online, but everyone either has both the interface and the class declaration in the same file, or uses 'include_once' or 'require_once', which to me seems to be referring to files, and not OOP. So what am I doing wrong here? Why can't my interface be found?
Remember that the use clause wants you to specify an absolute class/interface/trait name, not relative to your current namespace.
The below is wrong, unless your controller is in a global namespace (which I'm sure isn't the case):
use iTeamEntryController; // wrong
And this - below - is correct:
use App\Http\Controllers\iTeamEntryController;
If you keep your interface in app/Http/Controllers directory, don't forget to specify a namespace:
namespace App\Http\Controllers;
If you want, on the other hand, your interface to be in a root directory, make sure it is there. Then, the namespace is not needed and your use clause is correct. Except, I'm pretty sure you don't want your interfaces in the root directory of your Laravel app.
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
I'm using Laravel 5 framework.
I extended the Illuminate\Http\Request Class and added some functions. So I changed bootstrap to boot with my Custom Http Request Class. They work well.
But when it come to integrated test. by extending their TestCase, they use the Request class as below
use Illuminate\Http\Request;
$request = Request::create();
Is there a way for me to override their class from using Illuminate\Http\Request to use MyApp\Http\Request at my own class? I don't want to change their code.
No.
Their code calls it directly, so there's unfortunately nothing you can do.
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.
I'm totally new to namespaces, and Laravel in general, so their use of Facades complicates the issue a bit for me.
I have set up a class that is namespaced:
namespace Libraries;
class UploadedFile {
}
(As there is already a Symfony class uploadedFile), and now in that class I need to use one of my models, which I can only assume rests somewhere under the Eloquent facade, yet if I:
use Eloquent;
and
use \Eloquent;
in my class, I am told my model cannot be found, yet if I prepend my model with a backslash directly:
return \Object::create(...);
It works perfectly fine. What do I need to use at the top of my namespaced file to include access to my models directly without the need for a slash?
Eloquent has nothing to do with this. You have to import your actual model Object:
use Object;