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

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

WBOY
Release: 2016-08-08 09:29:41
Original
989 people have browsed it

Create laravel project

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><?<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>//Pass parameters to the controller, Route::get('/{id}')</span>

<span>//Two formats: 1. Route::get('/', function(){})</span>

<span>// 2. Route::get('/', array('as'=>'home_route',function(){})) as defined route name</span>

<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>//Pass parameters to the view</span></span>

<span> <span>//Method 1: </span></span>

<span> <span>//$var = 'hello world';</span></span>

<span> <span>//return View::make('hello')->with('var', $var);</span></span>

<span> <span>//Method 2</span></span>

<span> <span>//$var = 'abcd';</span></span>

<span> <span>//return View::make('hello', array('var'=>$var));</span></span>

<span> <span>//Method 3</span></span>

<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>//Define controller</span>

<span>Route<span>::<span>get<span>(<span>'index'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span>

<span>{</span>

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

<span> <span>'yunshu'<span>,</span></span></span>

<span> <span>'Yunshu'</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>$arr<span>));</span></span></span></span></span> </span></span></span> </span></span></span></span></span></span></span>

<span>});</span>

<span>//Generate routing URL and jump</span>

<span>Route<span>::<span>get<span>(<span>'test'<span>,<span> <span>function<span>()</span></span></span></span></span></span></span></span></span>

<span>{</span>

<span> <span>//Generate URL</span></span>

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

<span> <span>//Jump</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>

<span>});</span>

blade layout

(master.blade.php):

<span>@include<span>(<span>'layout.header'<span>)</span></span></span></span>

<span><body></span>

<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>Haha</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>Comment code--}}</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>Use <span>master<span>template<span> <span>--}}</span></span></span></span></span></span></span>

<span>{{--<span> <span>Use this part to populate the template<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>Create pictures<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>Overwrite or rewrite parent template content<span> <span>--}}</span></span></span></span></span>

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

<span> <span>{{--<span> <span>Get the content of the parent template and use it<span>@parent<span> <span>--}}</span></span></span></span></span></span></span>

<span> <span>@parent</span></span>

<span> <span>'Hello'</span></span>

<span>@stop</span>

Code packaging:

http://files.cnblogs.com/files/luoyunshu/learnlv.zip

The above introduces the first day of learning Laravel (creating laravel projects, routing, views, blade templates), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template