I am inputting a token as above and trying to print the token value with the below code. But getting error Undefined variable: token. I'm not sure whether i can access the token as below. Pls help me with ur suggestions.
<?php
namespace App\Http\Controllers;
use App\Models\Files;
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function upload()
{
$tock=Auth::user()->$token;
dd($tock);
}
}
You are trying to access the $token variable on the user, even though it does not exist.
Instead you should by trying to access the request and the values that are send with the request. This can be achieved using the request() helper or by injecting the Request class into the function.
With injection:
<?php
namespace App\Http\Controllers;
use App\Models\Files;
use Illuminate\Http\Request; // Add request to namespace
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function upload(Request $request)
{
dd($request->bearerToken());
}
}
Or, without injection:
<?php
namespace App\Http\Controllers;
use App\Models\Files;
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function upload()
{
dd($request->bearerToken());
}
}
With the bearerToken() method you can access the Bearer token provided in the request.
Related
route
Route::get('/token', [\App\Http\Controllers\TokenController::class, 'index']);
TokenController.php
namespace App\Http\Controllers;
use App\Services\Token;
class TokenController extends Controller
{
public function index()
{
$tokenService = app(Token::class);
return $tokenService->handle('https://www.nba.com');
}
}
Token Service
namespace App\Services;
class Token
{
public function handle(string $redirect)
{
return redirect($redirect)->withCookie(cookie('token', 'abcd'));
}
}
I want to leave a token in cookies when the user enters the page and redirect to another URL, I am sure the cookie was saved when I check on the browser, but I don't know to do the test that token service did save the cookie in the unit test.
first try
namespace Tests\Unit;
use App\Services\Token;
use Illuminate\Support\Facades\Cookie;
use Tests\TestCase;
class TokenTest extends TestCase
{
public function testToken()
{
app(Token::class)->handle('https://www.nba.com');
dd(Cookie::get('token')); // null
}
}
second try
namespace Tests\Unit;
use App\Services\Token;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
class TokenTest extends TestCase
{
public function testToken()
{
app(Token::class)->handle('https://www.nba.com');
Route::get('view-cookie', function (Request $request) {
return $request->cookie('token');
});
$response = $this->get('/view-cookie');
dd($response->getContent()); // ""
}
}
I create my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ResponsaveisController extends InternoController
{
}
I try to access Auth::user() multiple forms, all times return NULL
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Auth;
class InternoController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
var_dump(Auth::user());
}
}
Would anyone have any suggestions on how to solve this issue?
This is taken directly from the Laravel docs
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
You can read about it here as it provides an alternative solution.
Using php artisan make:request StoreUserData I've create my rules to the request:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserData extends FormRequest {
public function rules(){
return [
'name'=>'required|integer',
'surname'=>'required|max:255|string',
];
}
}
And I'm trying to use it in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDataController extends Controller {
public function store(StoreUserData $request){
return 'valid';
}
}
Here the error I get: Class App\Http\Controllers\StoreUserData does not exist.
PS. Is not a problem of routing.
I'm following this documentation since I'm using Laravel 5.6 https://laravel.com/docs/5.6/validation#creating-form-requests
Actually when you use StoreUserData in controller method then you have to import that class otherwise it will assume the class is in App\Http\Controllers namespace and thats why it throw Class App\Http\Controllers\StoreUserData does not exist.
just add the below import at top of your controller class
use App\Http\Requests\StoreUserData
I have following repository class
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Support\Facades\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>
and controller class
<?php
namespace App\Http\Controllers;
use App\Repositories\CustomerRepository;
use Illuminate\Http\Request;
class CustomerController extends Controller{
private $customerRepo;
function __construct(CustomerRepository $customerRepo){
$this->customerRepo = $customerRepo;
}
function index(Request $request){
return $this->customerRepo->getAll($request->input('search',null));
}
}
this gives me error
Type error: Argument 2 passed to
App\Repositories\CustomerRepository::getAll() must be an instance of
Illuminate\Support\Facades\Request, none given, called in
C:\xampp\htdocs\avmaxapi\app\Http\Controllers\CustomerController.php
on line 21
i know i havenot passed 2nd argument but should not it inject automatically
and i donot want to pass request object everytime from controller;
use use Illuminate\Http\Request;
and not: use Illuminate\Support\Facades\Request;
at the following:
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Http\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>
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