Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bhargav960143's avatar

How can i get resource store route laravel

My Add form code

<form method="post" name="m_form" action="{{url('post')}}">
<input type="hidden" name="_method" value="POST">
<input type="hidden" name="_token" value="zL9O2D3H70fkSrt9vziwbkf2bHBqi6O6NKk1QgQK">
<input type="text" required="" name="name" id="name" class="form-control m-input" >
<input type="submit" name="submit" value="submit">
<form>

My Route

Route::resource('post', 'PostController');

Middleware

 public function handle($request, Closure $next)
    {
        $nextRequest = $next($request);
        dd($request->route()->getAction());
    }

Now in Middleware "$request->route()->getAction()" is null

Now I want controller name and method name dynamically here all other route working fine but store route not found

Can you please guide me where I can make mistake?

Thanks.

0 likes
41 replies
markotitel's avatar

I am learner also. I will answer and read this as I see it.

Store is not a route but a method.

How does your PostController looks like, is there store method in the PostController?

Your Route also looks like a single action controller.

Lets see what is inside this PostController.

Bhargav960143's avatar

PostController

public function store(Request $request){
        $requested_data = $request->all();
        dd($requested_data);
    }

Before going to PostController I used middleware I need a route in the middleware.

markotitel's avatar

Should Route look like this?

Route::resource('post', 'PostController@store');

Ahh, sorry you have it as a resource.

Maybe try to dd($nextRequest....

public function handle($request, Closure $next)
    {
        $nextRequest = $next($request);
        dd($nextRequest->route()->getAction());
    }
markotitel's avatar

Strange it should work, but this middleware is still suspicious.

Can you try just dd ?

public function handle($request, Closure $next)
    {
        
        dd($request);
    }

That way we'll see full request data. And check if there is method name.

Snapey's avatar

what are you intending to do with middleware?

munazzil's avatar

Just type below command and display over here.

php artisan route:list
Bhargav960143's avatar

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.

munazzil's avatar

Have you used as like below in your PostController.

   public function __construct()
    {
          $this->middleware('AuthPermission'); 

    }

  public function store(Request $request)
   {
       $requested_data = $request->all();
      dd($requested_data);
   }
Bhargav960143's avatar

@munazzil

No not used like this.

construct not used.

  public function __construct()
    {
          $this->middleware('AuthPermission'); 

    }
Bhargav960143's avatar

@munazzil

the null response in middleware.

public function handle($request, Closure $next)
    {
        $nextRequest = $next($request);
        dd($request->route());
}

Controller

public function __construct()
    {
        $this->middleware('authpermission');
    }
Bhargav960143's avatar

@munazzil

If I set a route then it's work.

Route::post('post/store','admin\PostController@store');

Middleware

Route {#528 ▼
  +uri: "admin/post/store"
  +methods: array:2 [▶]
  +action: array:7 [▶]
  +isFallback: false
  +controller: PostController {#1473 ▶}
  +defaults: []
  +wheres: []
  +parameters: []
  +parameterNames: []
  +computedMiddleware: array:3 [▶]
  +compiled: CompiledRoute {#1368 ▶}
  #router: Router {#34 ▶}
  #container: Application {#5 ▶}
}

But if I want to use the default store method of resource then it's not work

Route::resource('post', 'admin\PostController');

null
munazzil's avatar

You have to use as like below and check i think with url that problem occured.

Route::resource('post/store', 'admin\PostController');

and form

<form method="post" name="m_form" action="{{url('post/store')}}">
munazzil's avatar

Use capital URL or Route not route.

           <form method="post" name="m_form" action="{{URL('post/store')}}">
munazzil's avatar

Remove the name="m_form" from that form tag.

munazzil's avatar

Required comes that single word only.

    <input type="text" required name="name" id="name" class="form-control m-input" >
munazzil's avatar

What is the laravel version? and php version?

Bhargav960143's avatar

Form name with or without it won't affect.

required="required" placed.

it's not working.

munazzil's avatar

use only required as like i mention.

required

munazzil's avatar

Can you show your PostController over here?

Bhargav960143's avatar

@MUNAZZIL - What things you need to install the software and how to install them

PHP >= 7.2.6

MYSQL >= 5.7

APACHE >= 2.4

LARAVEL >= 5.7

PHPSTROM >= 2018.3

PHPMYADMIN >= 4.7

Bhargav960143's avatar

@MUNAZZIL - My PostController

<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Route;
use Redirect;
use App\Post;

class PostController extends Controller
{
    public function __construct()
    {
        $this->middleware('authpermission');
    }
    /*
     * Display a listing of the post.
     */
    public function index()
    {
        $meta_title = trans('label.post_title');
        $meta_keyword = trans('label.post_keyword');
        $meta_description = trans('label.post_description');

        return view('admin.post.index', array(
            'meta_title' => $meta_title,
            'meta_description' => $meta_description,
            'meta_keyword' => $meta_keyword
        ));
    }

    /*
     * Display a listing of the post.
     */
    public function show()
    {
        $meta_title = trans('label.post_title');
        $meta_keyword = trans('label.post_keyword');
        $meta_description = trans('label.post_description');

        return view('admin.post.index', array(
            'meta_title' => $meta_title,
            'meta_description' => $meta_description,
            'meta_keyword' => $meta_keyword
        ));
    }

    /*
     * Create post
     */
    public function create(){
        $meta_title = trans('label.post_add_title');
        $meta_keyword = trans('label.post_add_keyword');
        $meta_description = trans('label.post_add_description');

        return view('admin.post.add', array(
            'meta_title' => $meta_title,
            'meta_description' => $meta_description,
            'meta_keyword' => $meta_keyword,
        ));
    }

    /*
     * Save post
     */
    public function store($request){
        $requested_data = $request->all();
        dd($requested_data);
    }
}
munazzil's avatar

use like below and check.

   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($guest)) {
                    if (! Gate::allows($permission_name)) {
                        return abort(401);
                    }
                }
            }
        }
    }
    return $nextRequest;
    }
munazzil's avatar

Store function use as like below you have to pass two param.

public function store(Request $request){
        $requested_data = $request->all();
        dd($requested_data);
 }
Bhargav960143's avatar

Hey @munazzil

public function handle($request, Closure $next)
    {
        $nextRequest = $next($request);
        dd($request->route());

$request->route() is null so display error while using $request->route()->getAction();

Next

Please or to participate in this conversation.