Home > Article > Backend Development > [Laravel] Basic use of Laravel laravel example tutorial laravel academy laravel framework download
[Laravel] Laravel's basic HTTP routing
Use Laravel's basic routing to implement the get request response. Find the file app/Http/routes.php
Call the static method get() of Route to implement the get response. Parameter: string type path, anonymous function function(){}
Inside the anonymous function, return string data
implement post, put, delete requests, the same as above
implement the route of get passing parameters, call the static method get() of Route, parameters : Path, anonymous function
Path, parameter name wrapped in braces, excluding $, for example: '/user/{id}'
Anonymous function, receiving parameters, for example: function($id){}
[Laravel ] Laraval's basic controller
In the app/Http/Controllers directory, create a new Index/IndexController.php
Define the namespace, namespace AppHttpControllersIndex
Introduce the Controller basic controller, use AppHttpControllersController
Define IndexController to inherit Controller
Implement the method index and return data
Define the behavior of the specified controller in the route, for example: Route::get("/index","IndexIndexController@index");,
Pay attention to the namespace part, the new controller is at the root Under the namespace, add your own new namespace when specified
[Laravel] Laravel's basic view
Under the directory resources/views/, create index/index.php
Use the function view() in the controller To call the template, parameters: file path (. separated directory), data
routes: routes.php
<?<span>php </span><span>/*</span><span>|-------------------------------------------------------------------------- | Routes File |-------------------------------------------------------------------------- | | Here is where you will register all of the routes in an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | </span><span>*/</span><span>/*</span><span>测试get post</span><span>*/</span><span> Route::get(</span>'/'<span>, function () { $url</span>=url("index"<span>); </span><span>return</span> "Hello World"<span>.$url; </span><span>//</span><span>return view('welcome');</span><span>}); Route::post(</span>"/post"<span>,function(){ </span><span>return</span> "测试post"<span>; }); </span><span>/*</span><span>传递参数</span><span>*/</span><span>Route::get(</span>"/user/{id}"<span>,function($id){ </span><span>return</span> "用户"<span>.$id; }); </span><span>/*</span><span>使用控制器</span><span>*/</span><span>Route::get(</span>"/index","Index\IndexController@index"<span>); </span><span>/*</span><span>|-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | This route group applies the "web" middleware group to every route | it contains. The "web" middleware group is defined in your HTTP | kernel and includes session state, CSRF protection, and more. | </span><span>*/</span><span> Route::group([</span>'middleware' => ['web'<span>]], function () { </span><span>// </span>});
controller: IndexController.php
<?<span>php namespace App\Http\Controllers\Index; use App\Http\Controllers\Controller; </span><span>class</span> IndexController <span>extends</span><span> Controller{ </span><span>public</span><span> function index(){ $data</span>=<span>array(); $data[</span>'title']="Index控制器"<span>; </span><span>return</span> view("index.index"<span>,$data); } }</span>
template: index.php
<span><</span><span>body</span><span>><span><</span><span>div </span><span>class</span><span>="container"</span><span>></span><span><</span><span>div </span><span>class</span><span>="content"</span><span>></span><span><</span><span>div </span><span>class</span><span>="title"</span><span>></span><span><?</span><span>php echo $title;</span><span>?></span><span></</span><span>div</span><span>></span><span></</span><span>div</span><span>></span><span></</span><span>div</span><span>></span><span></</span><span>body</span><span>></span>
The above introduces the basic use of [Laravel] Laravel, including laravel content. I hope it will be helpful to friends who are interested in PHP tutorials.