laravel是一个比较优雅的php框架,今天要改一个项目,所以小试了一下。
它的配置比较简单。先下载安装composer https://getcomposer.org/Composer-Setup.exe
安装过程中报错:The openssl extension is missing, which means that secure HTTPS transfers are impossible. If possible you should enable it or recompile php with --with-openssl
解决方法:找到path php路径下的php.ini,去掉;extension=php_openssl.dll前面的分号,重新安装成功。
下载laravel framework git clone https://github.com/laravel/laravel
进入项目目录 cd laravel
执行命令 composer install,会下载安装framework的依赖
执行命令 php artisan serve,运行lavarel development server.
访问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,成功显示名字列表