1

In my Laravel project I have this test:

    /** @test */
    public function tagControllerStore()
    {
        $this->post(action([TagController::class, 'store']), [
            'name' => 'My tag',
            'type' => 'My type',
            'color' => 'white'
        ])
        ->assertOk();
    }

This is my controller and the store method in it:

<?php

namespace Domain\Shared\Http\Controllers;


class TagController extends Controller
{

    /**
     * Store a newly created resource in storage.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(StoreTagRequest $request, Tag $tag)
    {
        $tag = Tag::create($request->validated());

        return response()->json([
            'id' => $tag->id
        ]);
    }
}

And the result is this:

InvalidArgumentException: Action Domain\Shared\Http\Controllers\TagController@store not defined.

How can I solve this issue?

Ferenc Bablena
  • 445
  • 1
  • 4
  • 12

1 Answers1

0

The action function will generate the url based on the defined routes, did you add a route with action [TagController::class, 'store']?

Zing
  • 1
  • 1
  • I added it in a middleware group with resources: Route::group(['middleware' => 'auth:api'], function () { Route::resources([ 'tag' => TagController::class ]); }); – Ferenc Bablena Oct 26 '21 at 11:13
  • If your controller route not declarations to use the standard PHP callable syntax, the `$namespace` property within your `RouteServiceProvider` will used to automatically prefix controller route declarations. – Zing Oct 26 '21 at 11:49
  • I deleted that prefix, and change the namespaces manually where it's necessary. – Ferenc Bablena Oct 26 '21 at 11:57
  • Then you may need to check the route cache file in `bootstrap/cache`, check the routes about `tag`. If the cache file does not exist, you can run `php artisan route:cache`. By the way, you'd better clear your route cache after deleting the prefix. – Zing Oct 26 '21 at 12:08