Now, we must understand how to create new pages with Laravel.
Let's take a look again at how the default homepage is defined:
routes/web.php:
use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome');});
To define the Route at the URL, you do Route::get()
.
- The first argument is the URL: in this example, it is
/
- The second argument is a callback function that returns a View.
View is a template file written in a Laravel-specific language called Blade. You can think of a Blade file as an HTML file with variables inside.
The welcome
part in the code means the file is in a resources/views
folder with the extension .blade.php
. This extension is crucial.
In that welcome.blade.php
file, we have HTML with some Blade syntax like @if
or @auth
. Let's change some text in the View.
resources/views/welcome.blade.php:
// ... <p class="mt-4 text-sm/relaxed"> Laravel has wonderful documentation covering every aspect of the framework. Whether you are a newcomer or have prior experience with Laravel, we recommend reading our documentation from beginning to end. Laravel framework has wonderful documentation covering every aspect of the framework. Whether you are a newcomer or have prior experience with Laravel, we recommend reading our documentation from beginning to end. </p>// ...
Reload the page. We can see a new word has been added without any recompiling or running terminal commands.
Now, let's add a second Route.
routes/web.php:
Route::get('/', function () { return view('welcome');}); Route::get('/second', function () { return view('second');});
This will make a project.test/second
link available.
The Route will return a View called second.blade.php
. Copy the file contents from welcome.blade.php
to second.blade.php
and modify some text.
resources/views/second.blade.php:
// ... <p class="mt-4 text-sm/relaxed"> Laravel framework has wonderful documentation covering every aspect of the framework. Whether you are a newcomer or have prior experience with Laravel, we recommend reading our documentation from beginning to end. Laravel second framework has wonderful documentation covering every aspect of the framework. Whether you are a newcomer or have prior experience with Laravel, we recommend reading our documentation from beginning to end. </p>// ...
Now, navigate to the /second
URL. We can see a different view loaded.
If you visit a Route that doesn't exist, a standard 404 will be shown.
That's it! That's how you create Routes and pages in Laravel with Blade language.
Of course, the pages will have more logic in real projects, but we will get to that a bit later. One step at a time.
But if you do have the case of similar static pages with just Blade file and without much logic, you can use a shorter syntax in the Routes file: instead of Route::get()
and a callback function, do Route::view()
and provide the Blade file name as a second parameter.
routes/web.php:
Route::get('/second', function () { return view('second'); }); Route::view('/second', 'second');
No comments or questions yet...