ホームページ > バックエンド開発 > PHPチュートリアル > Laravel学習初日(laravelプロジェクト、ルーティング、ビュー、ブレードテンプレートの作成)

Laravel学習初日(laravelプロジェクト、ルーティング、ビュー、ブレードテンプレートの作成)

WBOY
リリース: 2016-06-13 12:15:14
オリジナル
1428 人が閲覧しました

Laravel学習1日目(laravelプロジェクト、ルーティング、ビュー、ブレードテンプレートの作成)

laravelプロジェクトの作成

コンポーザー create-project laravel/laravel learnlv 4.1.*

ヘルプを表示:composer create-project

artisan ツール

を使用してキーを生成します: phpArtisan key:genrate その他のコマンドについては、 http://blog.luoyunshu.com/laravel-cheatsheet

ルート

route.php:

<span><?<span class="pln">php<div class="L0"><code class="language-php"><span class="pun"><?<span class="pln">php</span></span>

< span class= "com">/*<code class="language-php"><span class="com">/*</span>

<span class="com">|--------- --- --------------------------------------------------- --- ------------<code class="language-php"><span class="com">|--------------------------------------------------------------------------</span>

<span class="com">| 🎜>< /code><code class="language-php"><span class="com">| Application Routes</span>

<span class="com">|-------- --- --------------------------------------------------- --- --<div class="L5"><span class="com">|--------------------------------------------------------------------------</span>

<span class="com">|<div class="L6"><span class="com">|</span>

<span class="com">| ここで、アプリケーションのすべてのルートを登録できます。<div class="L7"><span class="com">| Here is where you can register all of the routes for an application.</span>

<span class="com">| 応答する URI を Laravel に伝えるだけです。<div class="L8"><span class="com">| It's a breeze. Simply tell Laravel the URIs it should respond to</span>

<span class="com">| その URI が要求されたときに実行するクロージャを指定します。<div class="L9"><span class="com">| and give it the Closure to execute when that URI is requested.</span>

<span class= "com" >|<div class="L0"><span class="com">|</span>

<span class="com">*/<div class="L1"><span class="com">*/</span>

<span class="com">// パラメーターをコントローラーに渡します、 Route::get('/{id}')<div class="L3"></ code><code class="language-php"><span class="com">//向控制器传递参数,Route::get('/{id}')</span>

<span class="com">//2 つの形式: 1. Route::get('/', function(){})<div class="L4">< /code><code class="language-php"><span class="com">//两种格式:1、Route::get('/', function(){})</span>

<span class="com">// 2. Route::get('/', array('as'=> 'home_route ',function(){})) 定義されたルート名として<div class="L5"> <code class="language-php"><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">関数<span class="pun">()<div class="L6"> <code class="language-php"><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>{</span>
<span class="pln"> <span class="com">//パラメータを渡すビュー <div class="L3"> <code class="language-php"><span class="pln"> <span class="com">//方法二</span></span>
<span class="pln"> <span class="com">//方法 1: <code class="language-php"><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">//$var = 'hello world' ;<div class="L6"><div class="L7"> <code class=" language-php"><span class="pln"> <span class="com">//return View::make (' hello')->with('var', $var);<code class="language-php"><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"> <span class="com">//方法 2<code class="language-php"><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="com">//$var = 'abcd';</span></span><span pln> <span class="com">//return View::make('hello', array('var'=>$var));</span></span> <span class="pln"> <span class="com">//方法 3</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"> -&gt ;<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):

@include('layout.header')<span class="lit">@include<span class="pun">(<span class="str">'layout.header'<span class="pun">)</span></span></span></span>
<span class="str"><body></span>
<span class="str"><body><code class="language-php"><span class="pln"> <span class="str"><div></span></span>
<span class="pln"> <span class="str"><div><code class="language-php"><span class="pln"> <span class="str"><div></span></span>
<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="str"><div><div class="L5"> <code class="language-php"><span class="pln"> <span class="pun"></<span class="pln">div<span class="pun">></span></span> @yield('content')
<span class="pln"> <span class="pun"></<span class="pln">div<span class="pun">></span></span>
<span class="pln"> <span class="str"><div></span></span> div>
<span class="pln"> <span class="str"><div></span></span> div> <span class="pln"> <span class="str"><div></span></span><span class="pln"> <span class="str"><div&gt ;</span></span>
<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 class="pln"> <span class="pun"></<span class="pln">div<span class="pun">></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

1 階Xiangzi Love Games
二次開発には laravel を使用します
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート