Home  >  Article  >  Backend Development  >  [Laravel] Basic use of Laravel laravel example tutorial laravel academy laravel framework download

[Laravel] Basic use of Laravel laravel example tutorial laravel academy laravel framework download

WBOY
WBOYOriginal
2016-07-29 08:54:20853browse

[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

php

/*|--------------------------------------------------------------------------
| 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.
|
*//*测试get post*/
Route::get('/', function () {
    $url=url("index");
    return "Hello World".$url;
    //return view('welcome');});
Route::post("/post",function(){
    return "测试post";
});

/*传递参数*/Route::get("/user/{id}",function($id){
    return "用户".$id;
});
/*使用控制器*/Route::get("/index","Index\IndexController@index");
/*|--------------------------------------------------------------------------
| 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.
|
*/
Route::group(['middleware' => ['web']], function () {
    //
});

controller: IndexController.php

php
namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
class IndexController extends Controller{
    public function index(){
        $data=array();
        $data['title']="Index控制器";
        return view("index.index",$data);
    }
}

template: index.php

<body><div class="container"><div class="content"><div class="title">php echo $title;?>div>div>div>body>

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn