Heim > Artikel > Backend-Entwicklung > Der erste Tag des Erlernens von Laravel (Erstellen von Laravel-Projekten, Routing, Ansichten, Blade-Vorlagen)
Laravel-Projekt erstellen
Komponist Create-Project Laravel/Laravel Learnlv 4.1.*
Hilfe anzeigen: Composer Create-Project
Verwenden Sie handwerkliche Werkzeuge
Generate key:php artisan key:genrate, weitere Befehle finden Sie unter: http://blog.luoyunshu.com/laravel-cheatsheet
Routenführung
route.php:
<code><span><?<span>php</span></span>
<span>/*</span>
<code><span>|--------------------------------------------------------------------------</span>
/*
<span>| Application Routes</span>
</p>|------------------------------------------------------ -------------------<p>
<code><span>|--------------------------------------------------------------------------</span>
<p>|. Anwendungsrouten<code><span>|</span>
<code><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>
</p>|<p>
<span>| and give it the Closure to execute when that URI is requested.</span>
<p>|. Hier können Sie alle Routen für eine Bewerbung registrieren.<code><span>|</span>
<code><span>*/</span>
|. Es ist ein Kinderspiel, Laravel die URIs mitzuteilen, auf die es antworten soll
<span>//向控制器传递参数,Route::get('/{id}')</span>
</p>| und geben Sie ihm den Abschluss, der ausgeführt werden soll, wenn dieser URI angefordert wird.<p>
<span>//两种格式:1、Route::get('/', function(){})</span>
<p>|<code><span>// 2、Route::get('/', array('as'=>'home_route',function(){})) as的定义路由名称</span>
<code><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>
<p>//Zwei Formate: 1. Route::get('/', function(){})<code><span> <span>//方法一:</span></span>
<code><span> <span>//$var = 'hello world';</span></span>
// 2. Route::get('/', array('as'=>'home_route',function(){})) als definierter Routenname
<span> <span>//return View::make('hello')->with('var', $var);</span></span>
::<span> <span>//方法二</span></span>
get
'/'<span> <span>//$var = 'abcd';</span></span>
,
(<span> <span>//return View::make('hello', array('var'=>$var));</span></span>
'as'
'home_route'<span> <span>//方法三</span></span>
,
function<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>
<code><span>Route<span>::<span>get<span>(<span>'index'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span>
{
<span>{</span>
//Parameter an die Ansicht übergeben<span> $arr <span>=<span> array<span>(</span></span></span></span>
<span> <span>'yunshu'<span>,</span></span></span>
//Methode 1: <span> <span>'云舒'</span></span>
<span> <span>);</span></span>
//$var = 'hello world';<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>
//return View::make('hello')->with('var', $var);<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>
//Methode 2<span>{</span>
<span> <span>//生成URL</span></span>
//$var = 'abcd';<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>
//return View::make('hello', array('var'=>$var));<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>
//Methode 3<span>});</span>
<p> $var <span data-wiz-span="data-wiz-span">=</span> </p>'def';<p></p>
<p></p>
<p></p>
<p></p>
<span>@include<span>(<span>'layout.header'<span>)</span></span></span></span>
=<span><body></span>
$view->var = $var;
return $view;
}));
//Controller definieren
Route::get('index', function()
{
$arr = array(
'yunshu',
'Yunshu'
);
return View::make('index.index',array('arr'=>$arr));
});
//Routing-URL generieren und springen
Route::get('test', function()
{
//URL generieren
$url = URL::route('home_route');
//echo $url;
//Jump
return Redirect::route('home_route');
});
Klingenlayout
(master.blade.php):
@include('layout.header')
<body>
<span> <span><div></span></span>
<span> <span><div></span></span>
<span> <span>@yield<span>(<span>'content'<span>)</span></span></span></span></span>
<span> <span></<span>div<span>></span></span>
<span> <span></<span>div<span>></span></span>
<span> <span><div></span></span>
<span> <span><div></span></span>
<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>div<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>
Code-Verpackung:
http://files.cnblogs.com/files/luoyunshu/learnlv.zip
Das Obige stellt den ersten Tag des Erlernens von Laravel vor (Erstellen von Laravel-Projekten, Routing, Ansichten, Blade-Vorlagen) und hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.