0

I'm using Laravel 8 for a new project and trying to use the laravel collective form helper. I want to submit to a method from my 'TransactionsController' from a form in my view.

However, when I try to open up the view so I can enter form values, I get action TransactionsController@store not defined although the controller and method exists and all. The page loads when I do the full route, as in: 'App\Http\Controllers\TransactionsController@store' in {!! Form::open(['action' => 'App\Http\Controllers\TransactionsController@store', 'method' => 'POST']) !!}, but when I set namespace to 'App\Http\Controllers' in RouteServiceProvider and do {!! Form::open(['action' => 'TransactionsController@store', 'method' => 'POST']) !!} , I get action 'App\Http\Controllers\TransactionsController@store' not defined. I don't get it.

Isn't it the same thing? What am I missing? How can I fix this and use {!! Form::open(['action' => 'TransactionsController@store', 'method' => 'POST']) !!} without getting that error? Thanks.

View (in a file named seller.blade.php)

...
{!! Form::open(['action' => 'TransactionsController@store', 'method' => 'POST']) !!}

    //Form groups and form elements

    {{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
{!! Form::close() !!}  

Route

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;
use App\Http\Controllers\TransactionsController;

Route::get('/', [PagesController::class, 'index']);

Route::get('/buyer', [PagesController::class, 'buyer']);

Route::get('/seller', [PagesController::class, 'seller']);

Route::get('/faqs', [PagesController::class, 'faqs']);

Route::get('/contact', [PagesController::class, 'contact']);

Route::resource('transactions', TransactionsController::class);

TransactionsController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Transaction;

class TransactionsController extends Controller
{

    public function index()
    {
        $transactions = Transaction::all();
        return view('transactions.index')->with('transactions', $transactions);
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
       //some form validations
    }

    public function show($id)
    {
        
    }

    public function edit($id)
    {
        //
    }


    public function update(Request $request, $id)
    {
        //
    }

   
    public function destroy($id)
    {
        //
    }
}

RouteServiceProvider

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * The controller namespace for the application.
     *
     * When present, controller route declarations will automatically be prefixed with this namespace.
     *
     * @var string|null
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60);
        });
    }
}

If it helps, I also get this when I try to run route:list

1    [internal]:0
      Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))

2   C:\wamp64\www\sbgi\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
      ReflectionException::("Class App\Http\Controllers\App\Http\Controllers\TransactionsController does not exist")
  • Welcome to SO ... can you show the route you defined that points to that action – lagbox Dec 10 '20 at 19:40
  • Just did now. Please check – Jeremiah Olu-Daniel Dec 11 '20 at 08:06
  • @lagbox Hi. Please could you help me take a look at it again? – Jeremiah Olu-Daniel Dec 12 '20 at 07:32
  • you can't set `protected $namespace` in the RouteServiceProvider and then use the Fully Qualified Class Names for the routes when defining them, or you will get the new error you are showing .. it is one or the other, not both – lagbox Dec 12 '20 at 17:35
  • @lagbox Oh okay. My bad. Well, that takes care of the terminal error but after taking `protected $namespace` out, I still get 'Action TransactionsController@store not defined' unless I use the FQCN in the form action...sadly. How frustrating. Guess I'll just use that then. – Jeremiah Olu-Daniel Dec 14 '20 at 06:08
  • because the `$namespace` var is used for prefixing namespaces to controllers when creating URLs to "actions" – lagbox Dec 14 '20 at 06:19
  • @lagbox I see. Thank you very much. That means I have to use some other method in the route. Is there a way I can use the same namespace in RouteServiceProvider in my route? Would this work? – Jeremiah Olu-Daniel Dec 14 '20 at 06:30
  • Fairly new to Laravel actually lol. Sorry if my questions or actions seem amateurish hehe. @lagbox – Jeremiah Olu-Daniel Dec 14 '20 at 06:35

0 Answers0