>  기사  >  백엔드 개발  >  [Laravel] Laravel的根本使用

[Laravel] Laravel的根本使用

WBOY
WBOY원래의
2016-06-13 12:29:08836검색

[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\[email protected]");,

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

 

[Laravel] Laravel的基本视图

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

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

 

路由:routes.php

 

<span style="color: #000000;">php</span><span style="color: #008000;">/*</span><span style="color: #008000;">|--------------------------------------------------------------------------| 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 style="color: #008000;">*/</span><span style="color: #008000;">/*</span><span style="color: #008000;">测试get post</span><span style="color: #008000;">*/</span><span style="color: #000000;"> Route::get(</span>'/'<span style="color: #000000;">, function () {    $url</span>=url("index"<span style="color: #000000;">);    </span><span style="color: #0000ff;">return</span> "Hello World"<span style="color: #000000;">.$url;    </span><span style="color: #008000;">//</span><span style="color: #008000;">return view('welcome');</span><span style="color: #000000;">});Route::post(</span>"/post"<span style="color: #000000;">,function(){    </span><span style="color: #0000ff;">return</span> "测试post"<span style="color: #000000;">;});</span><span style="color: #008000;">/*</span><span style="color: #008000;">传递参数</span><span style="color: #008000;">*/</span><span style="color: #000000;">Route::get(</span>"/user/{id}"<span style="color: #000000;">,function($id){    </span><span style="color: #0000ff;">return</span> "用户"<span style="color: #000000;">.$id;});</span><span style="color: #008000;">/*</span><span style="color: #008000;">使用控制器</span><span style="color: #008000;">*/</span><span style="color: #000000;">Route::get(</span>"/index","Index\[email protected]"<span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">|--------------------------------------------------------------------------| 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 style="color: #008000;">*/</span><span style="color: #000000;">Route::group([</span>'middleware' => ['web'<span style="color: #000000;">]], function () {    </span><span style="color: #008000;">//</span>});

 

控制器:IndexController.php

 

<span style="color: #000000;">phpnamespace App\Http\Controllers\Index;use App\Http\Controllers\Controller;</span><span style="color: #0000ff;">class</span> IndexController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Controller{    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> function index(){        $data</span>=<span style="color: #000000;">array();        $data[</span>'title']="Index控制器"<span style="color: #000000;">;        </span><span style="color: #0000ff;">return</span> view("index.index"<span style="color: #000000;">,$data);    }}</span>

 

模板:index.php

    <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>        <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span>            <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="content"</span><span style="color: #0000ff;">></span>                <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="title"</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #ff00ff;">php echo $title;</span><span style="color: #0000ff;">?></span><span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>            <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>        <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span></span></span></span></span>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.