Laravel學習第一天(創建laravel項目、路由、視圖、blade模板),laravelblade
創建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:
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
//向控制器傳遞參數,Route::get('/{id}')
//兩種格式:1、Route::get('/', function(){})
// 2、Route::get('/', array('as'=>'home_route',function(){})) as的定義路由名稱
Route::get('/', array('as'=>'home_route', function()
{
//向視圖傳遞參數
//方法一:
//$var = 'hello world';
//return View::make('hello')->with('var', $var);
//方法二
//$var = 'abcd';
//return View::make('hello', array('var'=>$var));
//方法三
$var = 'def';
$view = View::make('index.hello');
$view->var = $var;
return $view;
}));
//定義控制器
Route::get('index', function()
{
$arr = array(
'yunshu',
'雲舒'
);
return View::make('index.index', array('arr'=>$arr));
});
//生成路由URL與跳轉
Route::get('test', function()
{
//生成URL
$url = URL::route('home_route');
//echo $url;
//跳轉
return Redirect::route('home_route');
});
blade布局
(master.blade.php):
@include('layout.header')
<body>
<div>
<div>
@yield('content')
</div>
</div>
<div>
<div>
@section('section')
哈哈
@show
</div>
</div>
{{-- 注釋代碼--}}
@include('layout.footer')
index.blade.php:
@extends('layout.master')
{{-- 使用master模板 --}}
{{-- 使用這部分內容填充模板 --}}
@section('content')
@foreach($arr as $a)
{{ $a }}
@endforeach
{{-- 創建圖片 --}}
{{ HTML::image('image/1.jpg') }}
@stop
{{-- 覆蓋或者重寫父模板內容 --}}
@section('section')
{{-- 拿到父模板的內容使用@parent --}}
@parent
'你好呀'
@stop
代碼打包:
http://files.cnblogs.com/files/luoyunshu/learnlv.zip