1

My PagesController - Every time I click certain buttons on my page it gives me this error.

Target class [App\Http\Controllers\App\Http\Controllers\Admin\PagesController] does not exist.

<?php

namespace App\Http\Controllers\Admin;

use Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\WorkWithPage;
use App\Models\Page;
use Illuminate\Http\Request;

class PagesController extends Controller
{

    public function __construct() {
        $this->middleware('admin');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (Auth::user()->isAdminOrUser()) {
            $pages = Page::paginate(5);
        } else {
            $pages = Auth::user()->pages()->paginate(5);
        }
        return view('admin.pages.index', ['pages' => $pages]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.pages.create')->with(['model' => new Page()]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(WorkWithPage $request)
    {
        //
        Auth::user()->pages()->save(new Page($request->only([
            'title','url','content'])));

        return redirect()->route('pages.index')->with('status', 'The page has been created.');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function edit(Page $page)
    {
        if(Auth::user()->cant('update', $page)) {
            return redirect()->route('pages.index');
        }

        return view('admin.pages.edit', ['model' => $page]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function update(WorkWithPage $request, Page $page)
    {
        if(Auth::user()->cant('update', $page)) {
            return redirect()->route('pages.index');
        }
        $page->fill($request->only([
            'title','url','content']));

        $page->save();
        return redirect()->route('pages.index')->with('status', 'The page has been updated');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function destroy(Page $page)
    {
        if(Auth::user()->cant('delete', $page)) {
            return redirect()->route('pages.index');
        }
    }
}

I checked the page controller, the users controller, the routes, the providers for the last 2 days I cant do anything evene when I temporarily clear the error it comes back. The course I'm using for this doesn't show me what the problem is I wanted it over and over from the end to the beginning and the beginning to the end.

Here is the route file as well

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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');
});

Auth::routes();

Route::get('/admin', function() {
    return view('admin.index');
})->middleware('admin');

Route::resource('/admin/pages', 'App\Http\ControllersAdmin\PagesController', ['except' => [
    'show'
]]);

Route::resource('/admin/users', 'App\Http\Controllers\Admin\UsersController', ['except' => [
    'create','store','show'
]]);

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/home', 'HomeController@index')->name('home');
6
  • 3
    I think this is a mistake in your route file. Looks like you are missing an `\` at the start of your controller namespace. Could you add the route file, and what version of Laravel you are using? Commented Sep 9, 2021 at 12:21
  • I'm using Laravel 8, I just added the route file Commented Sep 9, 2021 at 12:26
  • What have you tried to resolve the problem? Where are you stuck? Commented Sep 9, 2021 at 12:38
  • App\Http\ControllersAdmin\PagesController - how should this be resolved to your controller that is stored in a different namespace? Commented Sep 9, 2021 at 12:38
  • 2
    I would advise you to use the new route syntax: Route::get('/user', [UserController::class, 'index']); . It would fix the namespace problem. Don't forget the use statement for the Controllers Commented Sep 9, 2021 at 12:48

2 Answers 2

1

If the class is in quotes, then you would use the path relative to \App\Http\Controllers

Route::resource('/admin/pages', 'Admin\PagesController', ['except' => [
    'show'
]]);

Route::resource('/admin/users', 'Admin\UsersController', ['except' => [
    'create','store','show'
]]);

Or you can use the full class path

Route::resource('/admin/pages', \App\Http\Controllers\Admin\PagesController::class, ['except' => [
    'show'
]]);

Route::resource('/admin/users', \App\Http\Controllers\Admin\UsersController::class, ['except' => [
    'create','store','show'
]]);
Sign up to request clarification or add additional context in comments.

4 Comments

Not sure why but the first route comes up with an error and it needs a comma, sorry I am quite the beginner with PHP and laravel
Oops, the full class paths needed double-colons, not single colons. Which first route were you having problems with, the quoted class or the unquoted class?
Just tried it but it seems to be giving me the same error. Maybe there's a problem in a different folder
What is the full error, complete with file and line info?
0

try this

Route::namespace('Admin')->group(function() {
    Route::resource('/admin/pages', 'PagesController');
});

2 Comments

Please add some explanation to your answer such that others can learn from it
Not sure why it isn't working but it came up with the same error maybe something in another file is the problem but I don't even know where to look I looked at all the controllers, providers and routes no clue what the problem is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.