Laravel学习第一天(创建laravel项目、路由、视图、blade模板)

WBOY
Lepaskan: 2016-08-08 09:29:41
asal
977 orang telah melayarinya

创建laravel项目

composer create-project laravel/laravel learnlv 4.1.*

查看帮助:composer create-project 

使用artisan工具

生成key:php artisan key:genrate,更多命令见:http://blog.luoyunshu.com/laravel-cheatsheet

路由

route.php:

<span><span>php</span></span>

<span>/*</span>

<span>|--------------------------------------------------------------------------</span>

<span>| Application Routes</span>

<span>|--------------------------------------------------------------------------</span>

<span>|</span>

<span>| Here is where you can register all of the routes for an application.</span>

<span>| It's a breeze. Simply tell Laravel the URIs it should respond to</span>

<span>| and give it the Closure to execute when that URI is requested.</span>

<span>|</span>

<span>*/</span>

<span>//向控制器传递参数,Route::get('/{id}')</span>

<span>//两种格式:1、Route::get('/', function(){})</span>

<span>// 2、Route::get('/', array('as'=>'home_route',function(){})) as的定义路由名称</span>

<span>Route<span>::<span>get<span>(<span>'/'<span>,<span> array<span>(<span>'as'<span>=><span>'home_route'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<span>{</span>

<span> <span>//向视图传递参数</span></span>

<span> <span>//方法一:</span></span>

<span> <span>//$var = 'hello world';</span></span>

<span> <span>//return View::make('hello')->with('var', $var);</span></span>

<span> <span>//方法二</span></span>

<span> <span>//$var = 'abcd';</span></span>

<span> <span>//return View::make('hello', array('var'=>$var));</span></span>

<span> <span>//方法三</span></span>

<span> $var <span>=<span> <span>'def'<span>;</span></span></span></span></span>

<span> $view <span>=<span> <span>View<span>::<span>make<span>(<span>'index.hello'<span>);</span></span></span></span></span></span></span></span></span>

<span> $view<span>-><span>var<span> <span>=<span> $var<span>;</span></span></span></span></span></span></span>

<span> <span>return<span> $view<span>;</span></span></span></span>

<span>}));</span>

<span>//定义控制器</span>

<span>Route<span>::<span>get<span>(<span>'index'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span>

<span>{</span>

<span> $arr <span>=<span> array<span>(</span></span></span></span>

<span> <span>'yunshu'<span>,</span></span></span>

<span> <span>'云舒'</span></span>

<span> <span>);</span></span>

<span> <span>return<span> <span>View<span>::<span>make<span>(<span>'index.index'<span>,<span> array<span>(<span>'arr'<span>=><span>$arr<span>));</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<span>});</span>

<span>//生成路由URL与跳转</span>

<span>Route<span>::<span>get<span>(<span>'test'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span>

<span>{</span>

<span> <span>//生成URL</span></span>

<span> $url <span>=<span> URL<span>::<span>route<span>(<span>'home_route'<span>);</span></span></span></span></span></span></span></span>

<span> <span>//echo $url;</span></span>

<span> <span>//跳转</span></span>

<span> <span>return<span> <span>Redirect<span>::<span>route<span>(<span>'home_route'<span>);</span></span></span></span></span></span></span></span></span>

<span>});</span>

blade布局

(master.blade.php):

<span>@include<span>(<span>'layout.header'<span>)</span></span></span></span>

<span></span>

<span> <span><div> <p><code><span> <span><div> <p><code><span> <span>@yield<span>(<span>'content'<span>)</span></span></span></span></span>

<span> <span><span>div<span>></span></span></span></span>

<span> <span><span>div<span>></span></span></span></span>

<span> <span><div> <p><code><span> <span><div> <p><code><span> <span>@section<span>(<span>'section'<span>)</span></span></span></span></span>

<span> <span>哈哈</span></span>

<span> <span>@show</span></span>

<span> <span><span>div<span>></span></span></span></span>

<span> <span><span>div<span>></span></span></span></span>

<span> <span>{{--<span> <span>注释代码--}}</span></span></span></span>

<span>@include<span>(<span>'layout.footer'<span>)</span></span></span></span>

index.blade.php:

<span>@extends<span>(<span>'layout.master'<span>)</span></span></span></span>

<span>{{--<span> <span>使用<span>master<span>模板<span> <span>--}}</span></span></span></span></span></span></span>

<span>{{--<span> <span>使用这部分内容填充模板<span> <span>--}}</span></span></span></span></span>

<span>@section<span>(<span>'content'<span>)</span></span></span></span>

<span> <span>@foreach<span>(<span>$arr <span>as<span> $a<span>)</span></span></span></span></span></span></span>

<span> <span>{{<span> $a <span>}}</span></span></span></span>

<span> <span>@endforeach</span></span>

<span> <span>{{--<span> <span>创建图片<span> <span>--}}</span></span></span></span></span></span>

<span> <span>{{<span> HTML<span>::<span>image<span>(<span>'image/1.jpg'<span>)<span> <span>}}</span></span></span></span></span></span></span></span></span></span>

<span>@stop</span>

<span>{{--<span> <span>覆盖或者重写父模板内容<span> <span>--}}</span></span></span></span></span>

<span>@section<span>(<span>'section'<span>)</span></span></span></span>

<span> <span>{{--<span> <span>拿到父模板的内容使用<span>@parent<span> <span>--}}</span></span></span></span></span></span></span>

<span> <span>@parent</span></span>

<span> <span>'你好呀'</span></span>

<span>@stop</span>

代码打包:

http://files.cnblogs.com/files/luoyunshu/learnlv.zip

以上就介绍了Laravel学习第一天(创建laravel项目、路由、视图、blade模板),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Artikel sebelumnya:PHP 命名空间 入门 Artikel seterusnya:failed (13: Permission denied) while reading upstream
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Isu terkini
Topik-topik yang berkaitan
Lagi>
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan