Home > Software design >  Target class [ProductController] does not exist
Target class [ProductController] does not exist

Time:12-20

web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

Route::post('/products/{qty}/add', 'ProductController@addProduct')->name('addProduct');

products-list.blade.php

<form action="{{ route('addProduct', 1) }}" method="post" >
  <input type="text" name="qty" value="1" />
  <button type="submit"  type="button">Add</button>
</form>

app/Http/Controllers/ProductController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function addProduct(Request $request, $qty)
    {
        dd($qty);
    }
}

But the controller class exists?

CodePudding user response:

Namespacing won't help in this scenario, because it's just a string:

Route::post('/products/{qty}/add', 'ProductController@addProduct')

This will work:

Route::post('/products/{qty}/add', [ProductController::class, 'addProduct'])

or you can do:

Route::post('/products/{qty}/add', 'App\Http\Controllers\ProductController@addProduct')
  • Related