Search code examples
laravelviewframeworks

Creating a new page in Laravel


I'm trying to create a new page in Laravel and I'm not sure what to do in the context of the Laravel framework. If it was just html, then you just create a new html file. In Laravel, what are all the things you need to do when creating a new page?


Solution

  • The easiest way in Laravel is to define a route closure, and return a view from it.

    In your routes/web.php file:

    Route::get('/my-page', function () {
        return view('my-page');
    });
    

    Then in resources/views you create a file called my-page.php, or if you want to use Laravel's Blade syntax (you probably do) call it my-page.blade.php.


    Edit: there's now an even easier way to to it. Also in routes/web.php:

    Route::view('/my-page', 'my-page');
    

    This will do exactly the same thing as the previous example, without the need for a closure and explicitly calling view().