Maison >développement back-end >tutoriel php >[Laravel] Laravel的基本使用,laravel使用_PHP教程

[Laravel] Laravel的基本使用,laravel使用_PHP教程

WBOY
WBOYoriginal
2016-07-12 08:54:53866parcourir

[Laravel] Laravel的基本使用,laravel使用

[Laravel] Laravel的基本HTTP路由

 

使用Laravel的基本路由,实现get请求响应,找到文件app/Http/routes.php

调用Route的静态方法get(),实现get响应,参数:string类型的路径,匿名函数function(){}

匿名函数内部,返回string数据

 

实现post,put,delete的请求,同上

 

实现get传递参数的路由,调用Route的静态方法get(),参数:路径,匿名函数

路径,大括号包裹参数名,不含$,例如:’/user/{id}’

匿名函数,接收参数,例如:function($id){}

 

[Laravel] Laraval的基本控制器

 

在app/Http/Controllers目录下,新建一个Index/IndexController.php

定义命名空间,namespace App\Http\Controllers\Index

引入Controller基本控制器,use App\Http\Controllers\Controller

定义IndexController继承Controller

实现方法index,返回数据

定义路由指定控制器的行为,例如:Route::get("/index","Index\IndexController@index");,

注意命名空间部分,新建的控制器是在根命名空间下面,指定的时候添加自己新加的命名空间

 

[Laravel] Laravel的基本视图

在目录resources/views/下面,创建index/index.php

在控制器中使用函数view()来调用模板,参数:文件路径(.分隔目录),数据

 

路由: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>});

 

控制器: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>

 

模板:index.php

    <span><</span><span>body</span><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>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1117666.htmlTechArticle[Laravel] Laravel的基本使用,laravel使用 [Laravel] Laravel的基本HTTP路由 使用Laravel的基本路由,实现get请求响应,找到文件app/Http/routes.php 调用R...
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn