Why "Request::" (Request Static class) gives me different methods from $resquest-> (Request object) in Laravel 5.4? - laravel

I was trying to grab the current path that was asked by the user.
Following the doc I have created these 2 methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
class TestController extends Controller
{
public function reqTest1(Request $req) {
dd($req->path());
}
public function reqTest2() {
dd(Request::path());
}
}
The reqTest1 works good, but I had to inject the Request object inside the function. There I can grab path() method, and I have the response as expected.
But if I dont wan to inject dependencies I should be able to get the same method only calling the static class Request. (I dont know).
I just realise that we dont have the same methods available in these 2 scenarios.
The methods list for the injected object is much bigger than the static class that generate the same object, for the Request class..
So, why Request class has different methods from a Request object created by a dependency injection?

A Laravel facade is a class which provides a static-like interface to services inside the container, i.e. every method you call statically on the Request facade is rerouted to the actual request object. This is the reason the Request facade is a object with few methods itself.
If you want to call the facade in your code change this line:
dd(Request::path());
to this:
dd(\Request::path());
or you are calling the static path() function on Illuminate\Http\Request not the Request facade, because of this line:
use Illuminate\Http\Request;
In my view, it's better to use the helper function request() instead of the \Request facade.

Related

use functtion in Controller extend

Controller 1:
namespace App\Http\Controllers;
class HomeController extends Controller
Controller 2:
namespace App\Http\Controllers\Games;
class ExchangeController extends Controller
i have problem when using function in controller, in 1st controller on same level as Controller is extended then $this->rollback method works, but with 2nd controller i cant access it but still can access If I rename the function, why can't I call the function named rollback in case 2 but can use in case 1

Laravel dependency injection without relying on the controller

I'm trying to figure out how to inject a dependency into a class in Laravel.
My structure:
SimpleController extends BaseController
{
public function example(SimpleModel $model, SimpleValidationRequest $request)
{
$result = $model->doStuff()
return $this->makeResponse($result);
}
}
SimpleModel extends Model
{
public function doStuff(ComplexService $service)
{
$service->doComplexLogic($this);
}
}
I have registered the ComplexService in my own service provider:
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(ComplexService::class);
}
}
I want to inject the service straight into the simpleModel's doStuff method, without having to inject it into the controller and then into the model. We're busy moving a monolithic application to Laravel and have service classes that contain all the complex business logic. Much of the logic is shared between different classes, so a controller method might call a model that calls a service that ends up making 4 or 5 calls to other services, and I want to be able to inject another service into any method that needs it without having to send it down from the controller all the way through to the bottom method that might need it.
Is there a way to do this? I have been looking online but everything I've found has required me to inject the service into the controller and then sending it through the application from there, which I want to avoid.
You can simply call the singleton inside the method via the app() helper
SimpleModel extends Model
{
public function doStuff(ComplexService $service)
{
app()->singleton(ComplexService::class)->doComplexLogic($this);
}
}
or recover the singleton by injecting it in the model via it's __construct() method.

How to use different method of a controller in laravel application

In my laravel application i should use some create and update method of some controller in another controller
According to my search is not a good thing to call a method from controller in another
I cant see the why don't call a controller method in another controller
I'm doing this way :
class Controller extends BaseController
{
protected $variable;
public function __construct()
{
$this->variable = "Hello";
}
}
and
class ClientController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
The __constructor is a magic method of class. It calls when you trying to create instance of class. So there is no way to use constructor without creating an instance or extendeding from another class. If you have a common code in different classes there a best way to use traits. thats give you an opportunity to include your trait and use methods ,making your code beutiful , flexible , readable following principes DRY,KISS.
you can create a base class with constructor and extend other controller of it
or you can put your code in to Http\Controllers\controller.php ('main controllers constructor')
also you can use trait

Laravel 5.3 Custom Class

Create a custom class in laravel when I am call in controller construct then
Auth::user() not return any data
When call from in a function then it's work
Class Code
<?php namespace App\Libraries;
use App\User;
use Auth;
use Illuminate\Support\Facades\DB;
use App\Friends;
class AppLibrarie
{
private static $friends_ids = array();
public function __construct()
{
self::$friends_ids=Auth::user();
}
public function getfriends(){
return self::$friends_ids;
}
}
And Controller
<?php
namespace App\Http\Controllers;
use App\Libraries\AppLibrarie;
use Illuminate\Http\Request;
use App\Http\Requests;
class LiveController extends Controller
{
protected $lib;
public function __construct(AppLibrarie $appLibrarie)
{
$this->lib = $appLibrarie;
}
public function search(Request $request){
return response()->json($this->lib->searchdata($request->get('query')));
}
}
Accessing authenticated user sessions has been deprecated in Laravel 5.3. Here is the paragraph in the upgrade guide
In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.
As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:
You will need to rethink your Authentication structure a bit if you are to upgrade

Method [validate] does not exist

Method validate does not exit. I tried to validate my form values as shown in the figure. But it give me this error. Please tell me . I am new to laravel.
[
You aren't extending the correct Controller.
class UserController extends Controller {
...
}
Controller then extends BaseController.
If you open up Controller which resides in the same namespace as your other controllers, you will see it uses the trait ValidatesRequests which is what provides the validate method.
You can also remove the line use Illuminate\Routing\Controller as BaseController;. There should be no reason to import that.

Resources