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,成功显示名字列表