Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Laravel学习第一天(创建laravel项目、路由、视图、blade模板),laravelblade_PHP教程

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

WBOY
WBOYasal
2016-07-13 10:06:441000semak imbas

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/957480.htmlTechArticleLaravel学习第一天(创建laravel项目、路由、视图、blade模板),laravelblade 创建laravel项目 composer create-project laravel/laravel learnlv 4.1.* 查看帮助:...
Kenyataan:
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 sebelumnya:php生成RSS订阅的方法_PHP教程Artikel seterusnya:PHP获取一年中每个星期的开始和结束日期的方法_PHP教程

Artikel berkaitan

Lihat lagi