首頁  >  文章  >  後端開發  >  Laravel學習第一天(創建laravel專案、路由、視圖、blade模板)

Laravel學習第一天(創建laravel專案、路由、視圖、blade模板)

WBOY
WBOY原創
2016-08-08 09:29:41973瀏覽

創建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>,,'as'<span>=><span>'home_route' <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>=

'def'

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

View

::make<span>(<span>'index.hello'<span>);<span><span><span><span><span><span>);</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><span></span></span></span></span>

<span> </span>

return $view<span>;</span>

<span> <span><span><span>}));<span><span><span> <span><span></span>//定義控制器</span></span></span> </span></span></span>Route</span>::</span>get(

'index'

, <span>function</span>()

<span><span>()<span><span></span></span> </span></span>{

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

<span> </span>

'yunshu'

,<span><span></span></span>

'雲舒'<span><span><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></span></span>

<span></span>

<span>});</span>

<span>//生成路由URL與跳轉<span><span><span> <span><span><span>Route<span>::<span>get</span>(</span>'test'</span>,</span> </span>function</span>()</span></span></span>

()<span></span>

{<span></span>

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

);<span></span>);

<span></span>);

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

return

Redirect<span>::</span>route(

'home_route');

});<span></span>

blade佈局

<span> </span>(master.blade.php):

🎜 🎜 🎜🎜🎜@include🎜(🎜'layout.header'🎜)🎜🎜🎜🎜🎜🎜 🎜🎜🎜🎜🎜🎜

<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>

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

$arr

as $a)<span></span>

<span></span>

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

{{-- 創建圖片 --}}<span></span> {{ HTML::

image

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

@stop<span></span> {{--

覆蓋或重寫父親範本內容

--}}<span></span>

@section

('section')<span> </span> {{-- 拿到父親模板的內容使用@parent

--}}

<span></span>

<span></span>

@parent<span></span>

'你好呀'

@stop

🎜 🎜🎜🎜程式碼打包:🎜🎜🎜 🎜http://files.cnblogs.com/files/luoyunshu/learnlv.zip🎜 🎜 以上就介紹了Laravel學習第一天(創建laravel專案、路由、視圖、blade模板),包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。 🎜 🎜 🎜

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 命名空間 入門下一篇:PHP 命名空間 入門

相關文章

看更多