skip to Main Content

I currently have a route that needs to link to a controller. The controller needs to give back the projects.blade.php file. It gives me the homepage instead.

These are the routes I currently have

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

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

This is what is in the controller currently:

class projects extends Controller
{
    public function index()
    {
        return view('projects');
    }
}

and then I have my blade file which just contains this:

@section('content')
    <main>
        <button href="upload.blade.php"> Make a project </button>
    </main>
@endsection

I am using a template file for the rest of my html. (No, I didn’t copy and paste the main into the template file I have, I did check for that)

Any idea of why this is happening and how I can fix this? I do also have a standard laravel login which I can link to without any problems.

2

Answers


  1. Route Names:
    It’s important to ensure that your routes have distinct names. In your code, both the homepage and projects routes have the name ‘homepage’, which might be causing conflicts. Change the name of the projects route to something unique like ‘projects’:

    Route::get('/', [AppHttpControllersHomepageController::class, 'index'])->name('homepage');
    Route::get('/projects', [AppHttpControllersHomepageController::class, 'index'])->name('projects');
    

    Button Link:
    The button in your blade file uses the href attribute, which is not the correct attribute for defining a link. Use the a tag for creating hyperlinks, and specify the correct URL using the href attribute:

    @section('content')
        <main>
            <a href="upload.blade.php">Make a project</a>
        </main>
    @endsection
    

    Route and Controller Names:
    Make sure your controller class is named correctly and follows Laravel’s naming conventions. It should be named ProjectsController (with a capital ‘P’) to match the Laravel convention for controllers. Also, ensure the controller is located in the correct directory:

    Controller name: ProjectsController.php
    Controller class: class ProjectsController extends Controller
    

    Update your controller accordingly:

    class ProjectsController extends Controller
    {
        public function index()
        {
            return view('projects');
        }
    }
    

    Blade File Location:
    Ensure that the projects.blade.php file is located in the correct directory within the resources/views folder. It should be in a folder structure that matches your controller’s namespace. For example, if your controller is in the AppHttpControllers namespace, the blade file should be in resources/views/projects.

    Login or Signup to reply.
  2. You have a couple of errors here.

    • Both of your routes executes the same controller function (HomePageController > index).
    • Your controller is not correct.

    First of all you should change your routes like this:

    Route::get('/', [AppHttpControllersHomepageController::class, 'index'])->name('homepage');
    
    Route::get('/projects', [AppHttpControllersProjectController::class, 'index'])->name('projects');
    

    After that, your controller will be:

    class ProjectController extends Controller
    {
        public function index()
        {
            return view('projects');
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search