Laravel 8: Call to undefined method Symfony\\Component\\HttpFoundation\\StreamedResponse::header() when download file - download

I want to write api to download a local file but whenever I try to download it it's showing this error:
"message": "Call to undefined method
Symfony\\Component\\HttpFoundation\\StreamedResponse::header()",
"exception": "Error",
"file": "C:\\xampp\\htdocs\\theme-store\\app\\Http\\Middleware\\Cors.php",
"line": 20,
api.php
Route::prefix('version')->group(function () {
Route::post('download', [VersionController::class, 'download']);
});
VersionController.php
public function download(Request $request){
return $this->versionService->download($request->id);
}
VersionService.php
public function download($id)
{
$version = Version::find($id)->makeVisible('download_link');
return Storage::download($version->download_link);
}
App\Http\Middleware\Cors.php
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*') //here is line 20
->header('Access-Control-Allow-Methods', '*')
->header('Access-Control-Allow-Headers','*');
}
How can I download file with Cors is enable

Related

How to fix this, i try to load dashboard route when the auth middleware is true

But when authentication was success, it shown error Route [/db1] not defined. I hace declared db1 route, but this route can access only if user has session. Anyone can tell me what wrong with my code?
this is my route:
Route::group(['middleware' => ['userSession']], function() { Route::get('/db1', [WasteController::class, 'db1'])->name('db1'); });
this is my kernel in middlewareGroup:
'userSession' => [ \App\Http\Middleware\CheckUserSession::class, ],
this is my middleware:
public function handle($request, Closure $next) {
if ($request->session()->get('status') != 'true') {
//status user cannot be found in session
return redirect('/');
}
return $next($request);
}
i have tried but it show error db1 route not defined
Did you try this?
public function handle($request, Closure $next) {
if ($request->session()->get('status') = 'true') {
//status user cannot be found in session
return $next($request);
}
return redirect('/');
}

How to avoid errors on nonfound model in method with model class as parameter?

When in laravel 8 I define destroy method with model class as parameter
public function destroy(Ad $ad)
{
$ad->delete();
return [];
}
on request in POSTMAN with non existing Ad I got not only 404 error code, but also error messages :
"message": "No query results for model [App\\Models\\Ad] 6",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"file": "/project//vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
"line": 385,
"trace": [
{
"file": "/project//vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
"line": 332,
"function": "prepareException",
"class": "Illuminate\\Foundation\\Exceptions\\Handler",
"type": "->"
},
If there is a way to deal it not to get errors in in POSTMAN output ?
Thanks in advance !
If you want to remove the extra detail from the body of the response, you can simply set APP_DEBUG=false.
If you also want to remove the message as well you can do this in your app/Exceptions/Handler.php class by overriding the render method:
use Illuminate\Database\Eloquent\ModelNotFoundException; // <-- add to the top of the class
public function render($request, Throwable $e)
{
if ($e instanceof ModelNotFoundException && $request->wantsJson()) {
return response('steve', 404);
}
return parent::render($request, $e);
}
Alternatively, if you also want to remove the message from the body for all NotFoundHttpExceptions, you could instead add the following to the register method:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; // <-- add to the top of the class
$this->renderable(function (NotFoundHttpException $e, $request) {
if ($request->wantsJson()) {
return response('', 404);
}
});
This will only change the result for JSON responses, the standard 404 page will still be used for non-JSON responses.
NB At the time of writing, you can't use the renderable method for intercepting a ModelNotFoundException as it gets converted to a NotFoundHttpException in the prepareException method before the renderable callbacks are executed.
public function destroy(Ad $ad): array
{
try{
$ad->delete();
} catch(ModelNotFoundException $modelNotFoundException) {
// do something here
} 
return [];
}
You could catch the ModelNotfoundException here.

debug adding header in middleware laravel

i added header in middleware and i want to check in my controller that header that i set exists or not . the problem i cant watch headers in controller debugging? how can i do that? anyway use case for this is cors problem
this is my middleware:
public function handle(Request $request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*');
}
and this is my controller:
public function action()
{
dd(request()->headers->get("Access-Control-Allow-Origin")); //always null
}
You need to call the set method:
public function handle(Request $request, Closure $next)
{
$request->headers->set('Access-Control-Allow-Origin', '*');
return $next($request);
}
Then you can retrieve the header:
public function action()
{
dd(request()->headers->get("Access-Control-Allow-Origin")); // *
}

Unable To Find the Solution

function login(Request $req)
{
$user= User::where("email",$req->input('email'))->get();
return Crypt::decrypt($user[0]->password);
}
then i put :
function login(Request $req)
{
$user= User::where("email",$req->input('email'))->get();
return Crypt::decrypt($user[0]->password)==$req->input('password');
}
I got the following error:
The Response content must be a string or object implementing __toString(), "boolean" given.

getting error "Undefined variable: guard" when redirect route in laravel 5.5

I am using middleware in 'laravel 5.5'. Everything is working fine, but when I am trying to redirect dashboard I am getting an error Undefined variable: guard.
I don't know what's wrong with this file.
middleware(RedirectIfNotGuestUser.php)
class RedirectIfNotGuestUser
{
public function handle($request, Closure $next)
{
if (!Auth::guard($guard)->check()) {
return redirect('user/login');
}
return $next($request);
}
}
Route.php
Route::group(['middleware' => ['web']], function () {
Route::get('user/login','Guestauth\LoginController#login');
Route::middleware(['guestuser'])->group(function () {
Route::get('account/dashboard', ['as'=>'account.dashboard','uses'=>'Guest\DashboardController#home']);
});
});

Resources