php


Laravel Middleware : Call to a member function header() on array


In my project there are two levels of middleware.
One UserAuthentication, and another is PermissionsMiddleWare.
Suppose there is a route :
Route::group(['middleware' => ['myauth', 'revalidate'], 'prefix' => '/user'], function () {
Route::group(['middleware' => 'permissions:Campaigns'], function () {
Route::resource('/dashboard', 'UserController#dashboard');
}
}
Now in UserAuthenticationMiddleware:
<?php
public function handle($request, Closure $next)
{
if ($request->session()->has('user_id')) {
$user_id = $request->session()->get('user_id');
} else {
return redirect('loginUser');
}
$response = $next($request);
return $response->header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate')
->header('Pragma', 'no-cache')
->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
return $next($request);
}
And in PermissionsMiddleware:
<?php
public function handle(Request $request, Closure $next, $permission_name = "")
{
//login to get permission decision
if (!$decision) {
// **Old process** of response
// return redirect('user/accessRejected')->with('message', 'Request rejected');
// **New process** of response
return ['accessRejected' => true, 'message' => 'Request rejected'];
}
$response = $next($request);
return $response->header('Cache-Control', 'nocache, no-store, max-age=0, must-revalidate')
->header('Pragma', 'no-cache')
->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
return $next($request);
}
Old process is working fine, and permissions middleware is restricting properly and redirecting to the page.
Now, as this is API side of project , so I cannot redirect to another page rather I need to response in JSON or array format.
When following the new process of response I am getting this error:
FatalErrorException in UserAuthenticationMiddleware.php line (this below code line):
-> return $response->header('Cache-Control','nocache, no-store, max-age=0, must-revalidate')
->header('Pragma','no-cache')
->header('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
Call to a member function header() on array
Please give some light on this to know what is wrong in my code.
Instead of returning only the data we can use return response with data. Like this :
return response(['accessRejected'=> true, 'message'=>'Request rejected reason:' . $this->checkPermission]);
}
Then we can get the response properly.

Related Links

Apache server does not recognize the htdocs folder
mysql to check if value is more than 0
How to transfer information between pages using onclick but without using the URl
PHP Email validator (form) from brackets
Paypal paying back users
Convert higher bitrate mp3 files to lower bitrate in PHP
Need help creating custom user registration/login script [closed]
Failing at Datatable's w/AJAX+jQuery
Redis as unique atomic id generator - Thread safe way for web app to avoid race condition
Azure Cloud Service Scaling - do I have to configure a Load Balancer?
Using a Tokenizer (strtok) to parse a string. How can I get the second token of the string?
PHP script from command prompt - is it possible to set \ read cookies?
MySQLI prepared statement giving me strange results
Why do i have to create another method to access a protected method from a parent class?
Is there a logical way to do this condition?
PHP Eclipse Luna on Mac doesn't have eclipse.ini

Categories

HOME
amazon-web-services
facebook-messenger
hdfs
visual-studio-2010
odata
consul
flurry
google-search-console
hystrix
slide
colors
amazon-emr
graphdb
orc
xbox360
anova
batch-rename
rxjs5
websphere-portal
environment
tracing
introspection
tf-idf
npoi
sales
netbios
nsmutablearray
shapeless
cloudera-manager
serversocket
android-canvas
nppexec
jade4j
django-crispy-forms
elastica
unification
microsoft-ui-automation
hidden-markov-models
stat
flipkart
permission-denied
type-inference
realstudio
dredd
phppgadmin
gestures
android-sharing
watir-webdriver
email-forwarding
ternary-operator
fuzzer
jsf-2.3
odp.net-managed
android-async-http
flipclock
currency-exchange-rates
pack
decidable
opencyc
enaml
blockquote
gql
darwin
scala-swing
camus
maven-shade-plugin
nanomsg
directorysearcher
dnsjava
dmake
qt5.4
jquery-mobile-flipswitch
communicate
treeline
map
smips
jmenubar
visual-c++-2005
infinite
unrealscript
codahale-metrics
void-pointers
itunes-sdk
xmlwriter
idispatch
image-scanner
prefuse
nsfont
getopts
office-automation
rtd
objectinstantiation
gui-designer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App