laravel是一個比較優雅的php框架,今天要改一個項目,因此小試了一下。php
它的配置比較簡單。先下載安裝composer https://getcomposer.org/Composer-Setup.exehtml
安裝過程當中報錯:The openssl extension is missing, which means that secure HTTPS transfers are impossible. If possible you should enable it or recompile php with --with-opensslmysql
解決方法:找到path php路徑下的php.ini,去掉;extension=php_openssl.dll前面的分號,從新安裝成功。laravel
下載laravel framework git clone https://github.com/laravel/laravelgit
進入項目目錄 cd laravelgithub
執行命令 composer install,會下載安裝framework的依賴正則表達式
執行命令 php artisan serve,運行lavarel development server.sql
訪問localhost:8000是項目的主頁數據庫
在app/routes.php新建一個routing瀏覽器
Route::get('users', function() { return 'Users!'; });
在瀏覽器訪問localhost:8000/user 便可看到"Users!"
新建一個視圖layout.blade.php:
<html> <body> <h1>Lavarel Quickstart</h1> @yield('content') </body> </html>
users.blade.php,使用laravel的模版系統-Blade,它使用正則表達式把你的模版轉換爲純的php:
@extends('layout') @section('content') Users! @stop
修改routes.php
Route::get('users',function(){ return View::make('users'); });
建立一個migration:
修改config/database.php中mysql的設置:
'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'laravel', 'username' => 'root', 'password' => '', 'charset' => 'utf8',
打開一個命令窗口運行:
php artisan migrate:make create_users_table
則在models下面會生成:user.php,在database/migrations下會生成一個migration,包含一個up和一個down方法,實現這兩個方法:
public function up() { // Schema::create('users',function($table){ $table->increments('id'); $table->string('email')->unique(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::drop('users'); }
在命令行運行:
php artisan migrate
在數據庫中爲表users添加幾條數據。
修改route.php中添加一行,並加上參數$users:
Route::get('users',function(){
$users = User::all();
return View::make('users')->with('users',$users);
});
修改視圖users.blade.php:
@extends('layout') @section('content') @foreach($users as $user) <p>{{$user->name}}</p> @endforeach @stop
在瀏覽器中訪問localhost:8000/users,成功顯示名字列表