My middleware file
@snapey
Use Middleware for roles and permission check.
Controller and its method allowed or not check for the specific role.
It's working all other method but for the default store method, it's not working.
Route response will be null
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Gate;
use Closure;
use Route;
class AuthPermission
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$nextRequest = $next($request);
$getActionName = $request->route()->getAction();
if(isset($getActionName['controller']) && !empty($getActionName['controller'])){
$getActionPath = $getActionName['controller'];
if(strpos($getActionPath, '@') !== false) {
$controller_path_array = explode('@',$getActionName['controller']);
$controller_name_with_path = current($controller_path_array);
if(strpos($controller_name_with_path, '\') !== false) {
$controller_split = explode('\',$controller_name_with_path);
$controller_name = end($controller_split);
$method_name = end($controller_path_array);
$permission_name = $controller_name . "_" . $method_name;
if (!app('auth')->guest()) {
if (! Gate::allows($permission_name)) {
return abort(401);
}
}
}
}
}
return $nextRequest;
}
}
@markotitel
Please check the response.
Request {#59 ▼
#json: null
#convertedFiles: null
#userResolver: null
#routeResolver: null
+attributes: ParameterBag {#69 ▶}
+request: ParameterBag {#68 ▶}
+query: ParameterBag {#67 ▶}
+server: ServerBag {#72 ▶}
+files: FileBag {#71 ▶}
+cookies: ParameterBag {#70 ▶}
+headers: HeaderBag {#73 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: array:6 [▶]
#pathInfo: "/admin/post/store"
#requestUri: "/admin/post/store"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: null
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}
@munazzil
In the route list, it's displayed.
| | POST | admin/post/store | admin. | App\Http\Controllers\Backend\PostController@store | web,auth |
Now, still its null response in middleware.