Home > Enterprise >  Laravel keeps saying "Target class [NewsletterController] does not exist.", I've trie
Laravel keeps saying "Target class [NewsletterController] does not exist.", I've trie

Time:04-28

I know there is a heck ton of links out there about Laravel not finding some class and stuff, but I really tried everything here and nothing works. I don´t even know which code I should share here to help, so...I'll share my Newsletter controller and the web.php code I'm using laravel 9.9 and php 8.1

web.php

<?php
Route::get('newsletter', 'App\Http\Controllers\NewsletterController@register');

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/', function () {
    return view('welcome');
});
Route::get('newsletter','NewsletterController@create');
Route::post('newsletter','NewsletterController@store');

Newsletter controler

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Newsletter;

class NewsLetterController extends Controller
{
    public function create()
    {
        return view('newsletter');
    }

    public function store(Request $request)
    {
        if ( ! Newsletter::isSubscribed($request->email) ) 
        {
            Newsletter::subscribePending($request->email);
            return redirect('newsletter')->with('success', 'Thanks For Subscribe');
        }
        return redirect('newsletter')->with('failure', 'Sorry! You have already subscribed ');
            
    }
}

CodePudding user response:

The issue is that your class name has a capital L for Letter, while on the route it does not, so your web.php should look like this:

<?php
Route::get('newsletter', 'App\Http\Controllers\NewsLetterController@register');

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/', function () {
    return view('welcome');
});
Route::get('newsletter','NewsLetterController@create');
Route::post('newsletter','NewsLetterController@store');

CodePudding user response:

Maybe I am not sure I think you can't use this method to write route in laravel 9 ...because I am using larvel 8 and it is not working so better to use: Route::get('/usernewsletter, [NewsLetterController::class, 'create']); https://laravel.com/docs/9.x/routing

  • Related