Home > database >  Controller gives error "Target class does not exist"
Controller gives error "Target class does not exist"

Time:10-07

I have a Laravel 8 project. I changed Controllers file location in project. I did almost all but only two Controllers getting errors. While changing the file locations, it seems that I have updated every necessary part, but it gives an error that I do not understand. It even shows the wrong location of the Controller in the error it gives. I leave a screenshot of the error below, you can see it from there.

enter image description here

The full file location of PromotionalCodeController is as follows: App\Http\Controllers\Admin\PromotionalCodeController

Although its location is like this, the error also shows this location differently.

enter image description here

web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\{
    AdminController,
    AwsTranscoderController,
    AwsTranscribeController,
    CategoryController,
    ContentController,
    Controller,
    CourseController,
    CreatorApplicationController,
    CreatorController,
    DevController,
    DynamicLinkController,
    FetchDataOutside,
    HomeController,
    LearnBiteController,
    NotificationsController,
    NotionApiController,
    PayoutController,
    RecomendationEngineController,
    RequestPayoutController,
    SubscriberController,
    TagController,
    UserTagController,
};

use App\Http\Controllers\Admin\Tools\{
    AnalyticsToolController,
    PayoutAlgorithmToolController,
};

use App\Http\Controllers\DevelopmentTools\{
    EmailTestController,
    GeneralToolsController,
};

/*
|--------------------------------------------------------------------------
| 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::prefix('admin-panel')->group(function(){

    Route::group(['middleware' => 'auth'], function(){

        Route::get('/home', [HomeController::class, 'index'])->name('home');

        Route::get('/contents', [ContentController::class, 'index'])->name('contents');

        Route::get('/courses/create', [CourseController::class, 'create'])->name('course_create');
        Route::post('/courses', [CourseController::class, 'store'])->name('course_store');
        Route::get('/courses', [CourseController::class, 'index'])->name('courses');
        Route::get('/courses/{id}', [CourseController::class, 'show'])->name('course_show');
        Route::delete('/courses/{id}', [CourseController::class, 'destroy'])->name('course_destroy');
        Route::get('/courses/{id}/edit', [CourseController::class, 'edit'])->name('course_edit');
        Route::put('/courses/{id}', [CourseController::class, 'update'])->name('course_update');
        Route::put('/courses/change_status/{id}', [CourseController::class, 'changeStatus'])->name('course_change_status');
        Route::post('/change-course-to-learnbite', [CourseController::class, 'convertToLearnbite'])->name('course_convert_to_learnbite');
        Route::post('/change-learnbite-to-course', [CourseController::class, 'convertToCourse'])->name('learnbite_convert_to_course');

        Route::get('/learnbites/create', [LearnBiteController::class, 'create'])->name('learnbite_create');
        Route::post('/learnbites', [LearnBiteController::class, 'store'])->name('learnbite_store');
        Route::get('/learnbites', [LearnBiteController::class, 'index'])->name('learnbites');
        Route::get('/learnbites/{id}', [LearnBiteController::class, 'show'])->name('learnbite_show');
        Route::delete('/learnbites/{id}', [LearnBiteController::class, 'destroy'])->name('learnbite_destroy');
        Route::get('/learnbites/{id}/edit', [LearnBiteController::class, 'edit'])->name('learnbite_edit');
        Route::put('/learnbites/{id}', [LearnBiteController::class, 'update'])->name('learnbite_update');
        Route::put('/learnbites/change_status/{id}', [LearnBiteController::class, 'changeStatus'])->name('course_change_status');

        Route::get('/course_categories/create', [CategoryController::class, 'create'])->name('course_categories_create'); 
        Route::post('/course_categories', [CategoryController::class, 'store'])->name('course_categories_store');
        Route::get('/course_categories', [CategoryController::class, 'index'])->name('course_categories');
        Route::get('/course_categories/{id}', [CategoryController::class, 'show'])->name('course_categories_show'); 
        Route::get('/course_categories/{id}/edit', [CategoryController::class, 'edit'])->name('course_categories_edit');
        Route::put('/course_categories/{id}', [CategoryController::class, 'update'])->name('course_categories_update');
        Route::delete('/course_categories/{id}', [CategoryController::class, 'destroy'])->name('course_categories_destroy');

        Route::get('/tags/create', [TagController::class, 'create'])->name('tags_create');
        Route::post('/tags', [TagController::class, 'store'])->name('tags_store');
        Route::get('/tags', [TagController::class, 'index'])->name('tags');
        Route::get('/tags/{id}', [TagController::class, 'show'])->name('tags_show');
        Route::get('/tags/{id}/edit', [TagController::class, 'edit'])->name('tags_edit'); 
        Route::put('/tags/{id}', [TagController::class, 'update'])->name('tags_update');
        Route::delete('/tags/{id}', [TagController::class, 'destroy'])->name('tags_destroy');

        Route::get('/user-tags/create', [UserTagController::class, 'create'])->name('user_tags_create');
        Route::post('/user-tags', [UserTagController::class, 'store'])->name('user_tags_store');
        Route::get('/user-tags', [UserTagController::class, 'index'])->name('user_tags');
        Route::get('/user-tags/{id}', [UserTagController::class, 'show'])->name('user_tags_show');
        Route::get('/user-tags/{id}/edit', [UserTagController::class, 'edit'])->name('user_tags_edit');
        Route::put('/user-tags/{id}', [UserTagController::class, 'update'])->name('user_tags_update');
        Route::delete('/user-tags/{id}', [UserTagController::class, 'destroy'])->name('user_tags_destroy');

        Route::get('/creators', [CreatorController::class, 'index'])->name('creators');
        Route::get('/creators/{id}', [CreatorController::class, 'show'])->name('creators_show');
        Route::put('/creators/{id}', [CreatorController::class, 'update'])->name('creators_update');
        Route::get('/creators/{id}/edit', [CreatorController::class, 'edit'])->name('creators_edit');
        Route::get('/creator/create', [CreatorController::class, 'create'])->name('creators_create');
        Route::post('/creators', [CreatorController::class, 'store'])->name('creators_store');
        Route::post('/creators-convert', [CreatorController::class, 'convert'])->name('creators_convert');
        Route::delete('/creators/{id}', [CreatorController::class, 'destroy'])->name('creators_destroy');

        Route::get('/creators/{id}/notes', [CreatorController::class, 'notes'])->name('creators_notes'); 
        Route::post('/creators/notes', [CreatorController::class, 'storeNote'])->name('post_creator_note');
        Route::post('/creators/balance', [CreatorController::class, 'addBalance'])->name('post_creator_balance');
        Route::put('/creators/notes/{id}', [CreatorController::class, 'updateNote'])->name('update_creator_note');
        Route::get('/creators/notes/{id}', [CreatorController::class, 'destroyNote'])->name('delete_creator_note');
        Route::post('/resend-create-profile-link', [CreatorController::class, 'resendCreateProfileLink'])->name('creators_resend_link');

        Route::get('/creator_applications', [CreatorApplicationController::class, 'index'])->name('creator_applications');
        Route::get('/creator_applications/{id}', [CreatorApplicationController::class, 'show'])->name('creator_application_show');
        Route::delete('/creator_applications/{id}', [CreatorApplicationController::class, 'destroy'])->name('creator_applications_delete');

        Route::get('/subscribers', [SubscriberController::class, 'index'])->name('subscribers');
        Route::get('/subscribers/{id}', [SubscriberController::class, 'show'])->name('subscribers_show');
        Route::get('/test-file-length/', [CourseController::class, 'testLength'])->name('testLength');

        Route::get('/notifications', [NotificationsController::class, 'create']);
        Route::post('/notifications', [NotificationsController::class, 'store']);

        Route::get('/analytics-tools', [AnalyticsToolController::class, 'index']); 
        Route::get('/payout-algorithm-tools', [PayoutAlgorithmToolController::class, 'index']);

        Route::post('/approve-request-payout', [RequestPayoutController::class, 'approveRequestedPayout'])->name('approve-requested-payout');

        Route::prefix('development-tools')->group(function() {
            Route::get('/email-test', [EmailTestController::class, 'index']);
            Route::get('/email-test-test', [EmailTestController::class, 'testEmail']);
            Route::post('/email-test', [EmailTestController::class, 'send']);
            Route::get('reset-avatar-resizing/{width}*{height}', [GeneralToolsController::class, 'clearThumbImage']);
            Route::get('non-listened-contents', [GeneralToolsController::class, 'getNonListenedContens']);
            Route::get('/token-check/{token}', [GeneralToolsController::class, 'isValidFireBaseMessagingToken']);
        });

        Route::prefix('payout')->group(function() {
            Route::get('/calculate', [PayoutController::class, 'calculate']);
        });

        Route::resource('promotional-codes', App\Http\Controllers\Admin\PromotionalCodeController)->names('promotionalcodes');

    });

});

Route::get('/', function () {
    return view('admin.welcome');
});

Auth::routes(['register' => false]);

Route::any('/dev/test', 'DevController@test');
Route::any('/dev/apple', 'DevController@apple');
Route::any('/dev/google', 'DevController@google');

Route::get('/creator_applications/getDatatable','CreatorApplicationController@getDataTable');
Route::get('/contents/getDatatable/{status_id?}','ContentController@getDataTable');
Route::get('/courses/getDatatable/{status_id?}','CourseController@getDataTable');
Route::get('/learnbites/getDatatable/{status_id?}','LearnBiteController@getDataTable');
Route::get('/course_categories/getDatatable','CategoryController@getDataTable');
Route::get('/tags/getDatatable','TagController@getDataTable');
Route::get('/user-tags/getDatatable','UserTagController@getDataTable');
Route::get('/creators/getDatatable','CreatorController@getDataTable');
Route::get('/creators/getDataTableProfile','CreatorController@getDataTableProfile');
Route::get('/subscribers/getDatatable','SubscriberController@getDataTable');
Route::prefix('cron')->group(function () {
    Route::get('/email', 'Crons\EmailCronJobController');
});

PromotionalCodeController

<?php

namespace App\Http\Controllers\Admin;

use App\PromoCode;
use Illuminate\Http\Request;

class PromotionalCodeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $codes = PromoCode::all();
        return view('admin.promotional_codes.index',['codes' => $codes]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.promotional_codes.create');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
           'promo_codes.code' => 'required|string',
           'promo_codes.max_usage' => 'required|numeric',
           'promo_codes.max_usage_per_user' => 'required|numeric',
           'promo_codes.valid_date' => 'required|string',
           'promo_codes.duration' => 'required|string',
        ]);

        $code = PromoCode::create($request->promo_codes);

        return redirect(route('promotionalcodes.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $code = PromoCode::findorfail($id);

        return view('admin.promotional_codes.show',['code' => $code]);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $code = PromoCode::findorfail($id);

        return view('admin.promotional_codes.edit',['code' => $code]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $code = PromoCode::findorfail($id);

        $request->validate([
            'promo_codes.code' => 'required|string',
            'promo_codes.max_usage' => 'required|numeric',
            'promo_codes.max_usage_per_user' => 'required|numeric',
            'promo_codes.valid_date' => 'required|string',
            'promo_codes.duration' => 'required|string',
        ]);
        $code->update($request->promo_codes);

        return redirect(route('promotionalcodes.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $code = PromoCode::findOrFail($id);
        $result = $code->delete();
        return $result;
    }
}

What am I missing?

CodePudding user response:

I played around with your setup a little: https://github.com/ancarofl/laravel-tests/commits/controller-does-not-exist. I can't reproduce the error but maybe it helps.

With your current web.php you'd have to wrap the controller name in qutoation marks:

Route::resource('promotional-codes', 'App\Http\Controllers\Admin\PromotionalCodeController');

Your original also works for me, barring the prefix and middlware:

Route::resource('promotional-codes', PromotionalCodeController::class);

My test project is in Laravel 9 but for Laravel 8 also have a look at
Hopex Development's comment regarding commenting protected $namespace = 'App\\Http\\Controllers';. I imagine the other controllers wouldn't work either if that were the problem though.

If nothing works please provide the output of php artisan route:list.

CodePudding user response:

It was hard, but I finally solved it out guys. In web.php I made the route of PromotionalCodeController as follows. I still can't understand the logic because Laravel 8's documentation does not say anything like that. So I think this is a bug with the resourse method in Laravel 8. But it was solved in the end, and that was the important thing.

I solved it like this:

The first state

Route::resource('promotional-codes', PromotionalCodeController::class)->names('promotionalcodes');

The second state

Route::namespace("\\")->group(function(){
            Route::resource('promotional-codes', PromotionalCodeController::class)->names('promotionalcodes');
        });

web.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\{
    AdminController,
    AwsTranscoderController,
    AwsTranscribeController,
    CategoryController,
    ContentController,
    Controller,
    CourseController,
    CreatorApplicationController,
    CreatorController,
    DevController,
    DynamicLinkController,
    FetchDataOutside,
    HomeController,
    LearnBiteController,
    NotificationsController,
    NotionApiController,
    PayoutController,
    PromotionalCodeController,
    RecomendationEngineController,
    RequestPayoutController,
    SubscriberController,
    TagController,
    UserTagController,
};

Route::prefix('admin-panel')->group(function(){

    Route::group(['middleware' => 'auth'], function(){ 

        Route::namespace("\\")->group(function(){
            Route::resource('promotional-codes', PromotionalCodeController::class)->names('promotionalcodes');
        });

If anyone has the same problem, I think they can fix it this way.

  • Related