Creating a new page in Laravel
Asked Answered
A

4

18

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?

Abilene answered 5/4, 2017 at 3:30 Comment(1)
Have a look at this series: laracasts.com/series/laravel-from-scratch-2017/episodes/2Cos
F
31

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().

Fluency answered 5/4, 2017 at 4:0 Comment(0)
A
4

There are two locations to add New Page to your Laravel project:

  1. You have to create an additional route in YOURAPP>routes>web.php file.
  2. You have to add PHP file with that name to YOURAPP>resources>views folder. If you want to use BLADE for your project, than you should put name_of_page.blade.php. And you should concider it as a must at the very beginning :).

Now, in routes you should be able to add only first part of the file rather than .blade.php. For example: about.blade.php, you can put in route only about

It should work out of the box :).

Alimentation answered 20/1, 2020 at 20:48 Comment(0)
C
1

You can also try this in routes/web.php

Route::get('/my-page', function () {
    return view('my-page');
})->name('my-page');
Chemosynthesis answered 6/12, 2021 at 12:6 Comment(0)
C
0

The function now seems to be in the HomeController.php file in the app\http\controllers directory.

public function page()
{
  $information = $this->metadataInformation();

  return view('page', compact('information'));
}

While the routes\web.php has the

Route::get('/page', [HomeController::class, 'page'])->name('page');
Choker answered 18/4, 2023 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.