官术网_书友最值得收藏!

Creating routes

The routes within Laravel are responsible for directing all HTTP traffic coming from the user's requests, so the routes are responsible for the entire inflow in a Laravel application, as we saw in the preceding diagrams.

In this section, we will briefly look at the types of routes available in Laravel, and how to create a simple route for our MVC component.

At this point, it is only necessary to look at how the routes work. Later in the book, we will get deeper into application routing.

So, let's look at what we can use to handle routes in Laravel:

   
Code            HTTP | METHOD |Verb

Route::get($uri, $callback);             GET

Route::post($uri, $callback);             POST

Route::put($uri, $callback);             PUT

Route::patch($uri, $callback);             PATCH

Route::delete($uri, $callback);             DELETE

Route::options($uri, $callback);            
OPTIONS

 

Each of the routes available is responsible for handling one type of HTTP request method. Also, we can combine more than one method in the same route, as in the following code. Do not be too concerned with this now; we'll see how to deal with this type of routing later in the book: 

Route::match(['get', 'post'], '/', function () {
    //
});

Now, let's create our first route:

  1. On your text editor, open web.php inside the routes folder, and add the following code, right after the welcome view:
Route::get('/band', function () {
return view('band');
});
  1. Open your browser to http://localhost:8081/band, and you will see the following message:

Hi i'm a view

Don't forget to start all Docker containers using the  docker-compose up -d command. If you followed the previous examples, you will already have everything up and running.

Bravo! We have created our first route. It is a simple example, but we have all of the things in place and working well. In the next section, we'll look at how to integrate a model with a controller and render the view.

主站蜘蛛池模板: 怀安县| 静宁县| 晋中市| 六盘水市| 平乐县| 公主岭市| 平顶山市| 弥勒县| 浦江县| 肃北| 汤阴县| 石嘴山市| 旬阳县| 青神县| 太谷县| 禄丰县| 合水县| 扶余县| 平邑县| 芜湖县| 焉耆| 屏边| 佛坪县| 法库县| 曲阳县| 桐庐县| 红安县| 巴林左旗| 沅陵县| 贡觉县| 昌吉市| 七台河市| 南通市| 怀远县| 丰台区| 济南市| 章丘市| 营口市| 顺平县| 平潭县| 漳平市|