ホームページ >php教程 >php手册 >Laravel学习第一天(创建laravel项目、路由、视图、blade模板),laravelblade

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

WBOY
WBOYオリジナル
2016-06-13 09:14:451045ブラウズ

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

创建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 class="pun"><span class="pln">php</span></span>   <span class="com">/*</span> <span class="com">|--------------------------------------------------------------------------</span> <span class="com">| Application Routes</span> <span class="com">|--------------------------------------------------------------------------</span> <span class="com">|</span> <span class="com">| Here is where you can register all of the routes for an application.</span> <span class="com">| It's a breeze. Simply tell Laravel the URIs it should respond to</span> <span class="com">| and give it the Closure to execute when that URI is requested.</span> <span class="com">|</span> <span class="com">*/</span>   <span class="com">//向控制器传递参数,Route::get('/{id}')</span> <span class="com">//两种格式:1、Route::get('/', function(){})</span> <span class="com">// 2、Route::get('/', array('as'=>'home_route',function(){})) as的定义路由名称</span> <span class="typ">Route<span class="pun">::<span class="kwd">get<span class="pun">(<span class="str">'/'<span class="pun">,<span class="pln"> array<span class="pun">(<span class="str">'as'<span class="pun">=><span class="str">'home_route'<span class="pun">,<span class="pln"> <span class="kwd">function<span class="pun">()</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> <span class="pun">{</span> <span class="pln"> <span class="com">//向视图传递参数</span></span> <span class="pln"> <span class="com">//方法一:</span></span> <span class="pln"> <span class="com">//$var = 'hello world';</span></span> <span class="pln"> <span class="com">//return View::make('hello')->with('var', $var);</span></span>   <span class="pln"> <span class="com">//方法二</span></span> <span class="pln"> <span class="com">//$var = 'abcd';</span></span> <span class="pln"> <span class="com">//return View::make('hello', array('var'=>$var));</span></span>   <span class="pln"> <span class="com">//方法三</span></span> <span class="pln"> $var <span class="pun">=<span class="pln"> <span class="str">'def'<span class="pun">;</span></span></span></span></span> <span class="pln"> $view <span class="pun">=<span class="pln"> <span class="typ">View<span class="pun">::<span class="pln">make<span class="pun">(<span class="str">'index.hello'<span class="pun">);</span></span></span></span></span></span></span></span></span> <span class="pln"> $view<span class="pun">-><span class="kwd">var<span class="pln"> <span class="pun">=<span class="pln"> $var<span class="pun">;</span></span></span></span></span></span></span> <span class="pln"> <span class="kwd">return<span class="pln"> $view<span class="pun">;</span></span></span></span> <span class="pun">}));</span>   <span class="com">//定义控制器</span> <span class="typ">Route<span class="pun">::<span class="kwd">get<span class="pun">(<span class="str">'index'<span class="pun">,<span class="pln"> <span class="kwd">function<span class="pun">()</span></span></span></span></span></span></span></span></span> <span class="pun">{</span> <span class="pln"> $arr <span class="pun">=<span class="pln"> array<span class="pun">(</span></span></span></span> <span class="pln"> <span class="str">'yunshu'<span class="pun">,</span></span></span> <span class="pln"> <span class="str">'云舒'</span></span> <span class="pln"> <span class="pun">);</span></span> <span class="pln"> <span class="kwd">return<span class="pln"> <span class="typ">View<span class="pun">::<span class="pln">make<span class="pun">(<span class="str">'index.index'<span class="pun">,<span class="pln"> array<span class="pun">(<span class="str">'arr'<span class="pun">=><span class="pln">$arr<span class="pun">));</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> <span class="pun">});</span>   <span class="com">//生成路由URL与跳转</span> <span class="typ">Route<span class="pun">::<span class="kwd">get<span class="pun">(<span class="str">'test'<span class="pun">,<span class="pln"> <span class="kwd">function<span class="pun">()</span></span></span></span></span></span></span></span></span> <span class="pun">{</span> <span class="pln"> <span class="com">//生成URL</span></span> <span class="pln"> $url <span class="pun">=<span class="pln"> URL<span class="pun">::<span class="pln">route<span class="pun">(<span class="str">'home_route'<span class="pun">);</span></span></span></span></span></span></span></span> <span class="pln"> <span class="com">//echo $url;</span></span>   <span class="pln"> <span class="com">//跳转</span></span> <span class="pln"> <span class="kwd">return<span class="pln"> <span class="typ">Redirect<span class="pun">::<span class="pln">route<span class="pun">(<span class="str">'home_route'<span class="pun">);</span></span></span></span></span></span></span></span></span> <span class="pun">});</span>

blade布局

(master.blade.php): <span class="lit">@include<span class="pun">(<span class="str">'layout.header'<span class="pun">)</span></span></span></span> <span class="str"></span> <span class="pln"> <span class="str"><div> <code class="language-php"><span class="pln"> <span class="str"><div> <code class="language-php"><span class="pln"> <span class="lit">@yield<span class="pun">(<span class="str">'content'<span class="pun">)</span></span></span></span></span> <span class="pln"> <span class="pun"><span class="pln">div<span class="pun">></span></span></span></span> <span class="pln"> <span class="pun"><span class="pln">div<span class="pun">></span></span></span></span>   <span class="pln"> <span class="str"><div> <code class="language-php"><span class="pln"> <span class="str"><div> <code class="language-php"><span class="pln"> <span class="lit">@section<span class="pun">(<span class="str">'section'<span class="pun">)</span></span></span></span></span> <span class="pln"> <span class="pun">哈哈</span></span> <span class="pln"> <span class="lit">@show</span></span> <span class="pln"> <span class="pun"><span class="pln">div<span class="pun">></span></span></span></span> <span class="pln"> <span class="pun"><span class="pln">div<span class="pun">></span></span></span></span> <span class="pln"> <span class="pun">{{--<span class="pln"> <span class="pun">注释代码--}}</span></span></span></span> <span class="lit">@include<span class="pun">(<span class="str">'layout.footer'<span class="pun">)</span></span></span></span> index.blade.php: <span class="lit">@extends<span class="pun">(<span class="str">'layout.master'<span class="pun">)</span></span></span></span> <span class="pun">{{--<span class="pln"> <span class="pun">使用<span class="pln">master<span class="pun">模板<span class="pln"> <span class="pun">--}}</span></span></span></span></span></span></span>   <span class="pun">{{--<span class="pln"> <span class="pun">使用这部分内容填充模板<span class="pln"> <span class="pun">--}}</span></span></span></span></span> <span class="lit">@section<span class="pun">(<span class="str">'content'<span class="pun">)</span></span></span></span> <span class="pln"> <span class="lit">@foreach<span class="pun">(<span class="pln">$arr <span class="kwd">as<span class="pln"> $a<span class="pun">)</span></span></span></span></span></span></span> <span class="pln"> <span class="pun">{{<span class="pln"> $a <span class="pun">}}</span></span></span></span> <span class="pln"> <span class="lit">@endforeach</span></span>   <span class="pln"> <span class="pun">{{--<span class="pln"> <span class="pun">创建图片<span class="pln"> <span class="pun">--}}</span></span></span></span></span></span> <span class="pln"> <span class="pun">{{<span class="pln"> HTML<span class="pun">::<span class="pln">image<span class="pun">(<span class="str">'image/1.jpg'<span class="pun">)<span class="pln"> <span class="pun">}}</span></span></span></span></span></span></span></span></span></span> <span class="lit">@stop</span>   <span class="pun">{{--<span class="pln"> <span class="pun">覆盖或者重写父模板内容<span class="pln"> <span class="pun">--}}</span></span></span></span></span> <span class="lit">@section<span class="pun">(<span class="str">'section'<span class="pun">)</span></span></span></span> <span class="pln"> <span class="pun">{{--<span class="pln"> <span class="pun">拿到父模板的内容使用<span class="lit">@parent<span class="pln"> <span class="pun">--}}</span></span></span></span></span></span></span> <span class="pln"> <span class="lit">@parent</span></span> <span class="pln"> <span class="str">'你好呀'</span></span> <span class="lit">@stop</span>   代码打包: http://files.cnblogs.com/files/luoyunshu/learnlv.zip
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:自己写的php curl库实现整站克隆功能,phpcurl次の記事:PHP保存带BOM文件的方法,

関連記事

続きを見る