Home >Backend Development >PHP Tutorial >The first day of learning Laravel (creating laravel projects, routing, views, blade templates), laravelblade_PHP tutorial

The first day of learning Laravel (creating laravel projects, routing, views, blade templates), laravelblade_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:441039browse

The first day of learning Laravel (creating laravel projects, routing, views, blade templates), laravelblade

Creating laravel projects

composer create-project laravel/laravel learnlv 4.1.* View help: composer create-project ​ Use artisan tools Generate key:php artisan key:genrate. For more commands, see: http://blog.luoyunshu.com/laravel-cheatsheet ​

Routing

route.php: <span class="pun"><?<span class="pln">php</span></span><span class="com">/*</span>php<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>| Application Routes<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>| Here is where you can register all of the routes for an application.<span class="pun">{</span> <span class="pln"> <span class="com">//向视图传递参数</span></span>| It's a breeze. Simply tell Laravel the URIs it should respond to<span class="pln"> <span class="com">//方法一:</span></span> <span class="pln"> <span class="com">//$var = 'hello world';</span></span>| and give it the Closure to execute when that URI is requested.<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>//Pass parameters to the controller, Route::get('/{id}')<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>//Two formats: 1. Route::get('/', function(){})<span class="pln"> <span class="kwd">return<span class="pln"> $view<span class="pun">;</span></span></span></span> <span class="com">// 2. Route::get('/', array('as'=>'home_route',function(){})) as defined route name<span class="pun"></span></span> Route::get('/', array('as'=>'home_route', function() { //Pass parameters to the view //Method 1: //$var = 'hello world'; //return View::make('hello')->with('var', $var); ​ //Method 2 //$var = 'abcd'; //return View::make('hello', array('var'=>$var)); ​ //Method 3 $var = 'def'; $view = View::make('index.hello'); $view->var = $var ; return $view; }));<span class="com">//定义控制器</span>//Define controller<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>Route<span class="pln"> <span class="str">'yunshu'<span class="pun">,</span></span></span>::get<span class="pln"> <span class="str">'云舒'</span></span>('index'<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> function<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>

{

<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="pln"> <span class="str"><div></span></span> $arr <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> array<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="str"><div></span></span><span class="pln"> <span class="str"><div></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>
'yunshu'<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 class="str">'Yunshu' ); <span class="kwd">return <span class="typ">View::make(<span class="str">'index .index', array(<span class="str">'arr'=>$arr)); }); ​ <span class="com">//Generate routing URL and jump <span class="typ">Route::<span class="kwd">get(<span class="str">'test', <span class="kwd">function() { <span class="com">//Generate URL $url = URL::route(<span class="str">'home_route'); <span class="com">//echo $url; ​ <span class="com">//Jump <span class="kwd">return <span class="typ">Redirect::route(<span class="str">'home_route '); }); blade layout (master.blade.php): <span class="lit">@include(<span class="str">'layout.header') <span class="str"><body> <span class="str"><div> <span class="str"><div> <span class="lit">@yield(<span class="str">'content') </<🎜>div<🎜>> </<🎜>div<🎜>> ​ <span class="str"><div> <span class="str"><div> <span class="lit">@section(<span class="str">'section') Haha <span class="lit">@show </<🎜>div<🎜>> </<🎜>div<🎜>> {{-- Comment code--}}</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></span></span></span></span></span></span></span> <span class="lit">@include<span class="pun">(<span class="str">'layout.footer'<span class="pun">)<code class="language-php"><span class="lit">@include<span class="pun">(<span class="str">'layout.footer'<span class="pun">)</span></span></span></span><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> index.blade.php: <span class="lit">@extends<span class="pun">(<span class="str">'layout.master'<span class="pun">)<code class="language-php"><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="pun">{{--<span class="pln"> <span class="pun">使用<span class="pln">master<span class="pun">模板<span class="pln"> <span class="pun">--}}<code class="language-php"><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>   <span class="pun">{{--<span class="pln"> <span class="pun">使用这部分内容填充模板<span class="pln"> <span class="pun">--}}<span data-wiz-span="data-wiz-span"></span></span><p align="left"></p> <div style="display:none;"> <code class="language-php"><span class="lit">@section<span class="pun">(<span class="str">'content'<span class="pun">)<span id="url" itemprop="url"></span><span id="indexUrl" itemprop="indexUrl"></span><span id="isOriginal" itemprop="isOriginal"> <code class="language-php"><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 id="isBasedOnUrl" itemprop="isBasedOnUrl"></span><span id="genre" itemprop="genre"></span><span id="description" itemprop="description"></span></span></span></span></span></span></span>
<span class="pln"> <span class="pun">{{<span class="pln"> $a <span class="pun">}}<div class="art_confoot"></div> <code class="language-php"><span class="pln"> <span class="lit">@endforeach   <code class="language-php"><span class="pln"> <span class="pun">{{--<span class="pln"> <span class="pun">创建图片<span class="pln"> <span class="pun">--}} <code class="language-php"><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">}} <code class="language-php"><span class="lit">@stop   <code class="language-php"><span class="pun">{{--<span class="pln"> <span class="pun">覆盖或者重写父模板内容<span class="pln"> <span class="pun">--}} <code class="language-php"><span class="lit">@section<span class="pun">(<span class="str">'section'<span class="pun">) <code class="language-php"><span class="pln"> <span class="pun">{{--<span class="pln"> <span class="pun">拿到父模板的内容使用<span class="lit">@parent<span class="pln"> <span class="pun">--}} <code class="language-php"><span class="pln"> <span class="lit">@parent <code class="language-php"><span class="pln"> <span class="str">'你好呀' <code class="language-php"><span class="lit">@stop   代码打包: http://files.cnblogs.com/files/luoyunshu/learnlv.zip http://www.bkjia.com/PHPjc/957480.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/957480.htmlTechArticleLaravel学习第一天(创建laravel项目、路由、视图、blade模板),laravelblade 创建laravel项目 composer create-project laravel/laravel learnlv 4.1.* 查看帮助:... </span>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to generate RSS subscription in php_PHP tutorialNext article:How to generate RSS subscription in php_PHP tutorial

Related articles

See more