In this post, we are going to explain how to create new routes in Laravel, what they are and how to use the controllers and views.
If we access to the directory where we created our new Laravel project, we will see that there are many directories. Each directory has many files with its functionality.
- app: where are all the classes of our app. (controllers, models, etc.)
- bootstrap: contains what is necessary to bootstrap the framework
- config: contains the configuration files
- database: contains the migrations and seeds
- public: contains the index.php that is the file where all the requests go
- resources: contains the views and no-compiled assets (JS, CSS, etc)
- routes: contains your app routes
- storage: contains cache and logs
- tests: contains tests
- vendor: contains the dependencies
Routes
The routes app are in the file routes/web.php if it is a web route, if it was a route for an API it should be in routes/api.php. You can add as much routes as you like, but if you access to a route and this one isn’t in these files, Laravel will throw an error 404.
We are going to create our first GET route. We need to open the file, add the next code and save it:
1 2 3 |
Route::get('/test', function () { return 'This is our new route.'; }); |
Open the browser and go to localhost:8000/test and we will see the sentence we put.
Laravel also allows us to use any HTTP method, simply changing get by post, put, delete… This we will use it later when we work with forms.
Sometimes you need to access to a specific object to show it, for example, when we want to show the user profile or a post. For this, we are going to add another route in the same file.
1 2 3 |
Route::get('/user/{name}', function ($name) { return 'The user name is '.$name; }); |
We access from the browser to localhost:8000/user/peter and we will see the sentence, if we modify the name in the url we can see that the sentence changes as well.
Views
The views is what you see when you access to some website. The Laravel views are in resources/views. To see an example of how they work, on the example code of the routes you can see it.
1 2 3 |
Route::get('/', function () { return view('welcome'); }); |
When you enter to the root url (/) the view welcome is showed. We can find it at resources/views. If you open we will see that there are some parts with a different syntax, this is Blade, the Laravel template engine. Later we will go deeper into it and learn about it.
Controllers
The controllers are files that send data to the views. To create a controller you can do it from the command line or manually, I think is simpler from the command line.
1 |
php artisan make:controller PostsController |
Once executed, Laravel will create the controller in app/Http/Controllers , if we open it we will see that we have everything ready to add the methods.
We are going to create a method, also we will have to change the way to add the routes, now we see it.
Add the method show to the controller previously created:
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PostsController extends Controller { public function show($id) { return view('post', ['post_id' => $id]); } } |
In the method we are catching the id passed from the route and we pass it to the view to show it:
1 |
<p>The post id is {{ $post_id }}</p> |
Lastly, add the route, the new way means from the controller PostsController “execute” the method show:
1 |
Route::get('/post/{id}', 'PostsController@show'); |
Now we can access to localhost:8000/post/3 , for example, and we will see how everything works fine and the sentence changes dynamically depending on the parameter.
The next post will be talking about controllers and views together with models. Which will cause us to configure the connection to the database in order to use the models.