#进入网站根目录创建项目目录
#clone lavarel 库
composer create-project laravel/laravel sylblog
(创建4.2版本是:composer create-project laravel/laravel 4.2 sylblog)
composer 命令参照:segmentfault.com/a/1190000000355928
#修改storage 权限
cd sylblog/ chmod -R 777 storage/
#配置本地二级域名
ngnix 配置域名和二级域名
注意:root 目录是public 如: root /usr/share/nginx/html/sylblog/public;
#打开:sylblog.test.com
显示 logo 即是成功!
laravel 已经集成genarate 到artisan,不需要再安装genarate插件
#配置数据库
nano .env
#使用phpmyadmin 新建 sylblog数据库 编码为utf8-genaral-ci
#创建数据库迁移文件
laravel 自带autor 系统,为了学习该教程:删掉默认User 模型,app/controller/author,app/User,database/migrations/ 里两张迁移文件
#重新创建用户模型
php artisan make:model User
这样创建了 app/User.php 和 XXX.users.php
#修改迁移文件
public function up() { Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->string('email')->nullable(); $table->string('password')->nullable(); $table->string('nickname')->nullable(); $table->string('is_admin')->default(0); $table->string('block')->default(0); $table->timestamps(); }); }nullable(),default 是laravel 的特有的方法
文档:laravel-china.org/docs/5.0/schema
#执行数据迁移
php artisan migrate
这样数据库创建了两张表 users 和migrations
migrate 相关命令
www.cnblogs.com/huangbx/p/Laravel_3.html
blog.ninja911.com/blog-show-blog_id-75.html
#填充seeder
在database/seeds 下面创建 UserTabelSeeder.php 文件
编辑
use Illuminate\Database\Seeder; use App\User; class UserTableSeeder extends Seeder{ public function run(){ DB::table('users')->delete(); User::create([ 'email' => '[email protected]', 'password' => Hash::make(''), 'nickname' => 'admin', 'is_admin' => 1, ]); } }
添加
$this->call('UserTableSeeder');
#执行填充
composer dump-autoload php artisan db:seed
composer dump-autoload 自动加载
即添加一条数据
#创建视图模板
在resource/views 创建模板文件
_layouts/default.blade.php
_layouts/footer.blade.php
_layouts/nav.blade.php
index.blade.php
#修改default.blade.php
<!DOCTYPE html> <html> <head lang="zh"> <meta charset="UTF-8"/> <title>ShiYanLou Blog</title> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="format-detection" content="telephone=no"/> <meta name="renderer" content="webkit"/> <meta http-equiv="Cache-Control" content="no-siteapp"/> <link rel="alternate icon" type="image/x-icon" href="{{ URL::asset('i/favicon.ico') }}"/> <link rel="stylesheet" href="//cdn.amazeui.org/amazeui/2.1.0/css/amazeui.min.css"/> {!! Html::style{'custom.css'} !!} </head> <body> <header class="am-topbar am-topbar-fixed-top"> <div class="am-container"> <h1 class="am-topbar-brand"> <a href="/">ShiYanLou Blog</a> </h1> @include('_layouts.nav') </div> </header> @yield('main') @include('_layouts.footer') <script src="//cdn.bootcss.com/jquery/2.1.3/jquery.min.js"></script> <script src="//cdn.amazeui.org/amazeui/2.1.0/js/amazeui.min.js"></script> </body> </html>
{{HTML::style('css/custom.css')}}在laravel 5中以移除 参照laravel 添加html 和form 标签
#修改nav.blade.php
<button class="am-topbar-btn am-topbar-toggle am-btn am-btn-sm am-btn-secondary am-show-sm-only" data-am-collapse="{target: '#collapse-head'}"><span class="am-sr-only">nav switch</span> <span class="am-icon-bars"></span></button> <div class="am-collapse am-topbar-collapse" id="collapse-head"> <div class="am-topbar-right"> <a href="#" class="am-btn am-btn-primary am-topbar-btn am-btn-sm topbar-link-btn"><span class="am-icon-user"></span> Login</a> </div> </div>
<footer class="footer"> <p>© 2015 By <a href="#" target="_blank">www.shiyanlou.com</a></p> </footer>
@extends('_layouts.default') @section('main') <div class="am-g am-g-fixed blog-g-fixed"> <div class="am-u-sm-12"> <h1>Welcome to ShiYanLou!</h1> </div> </div> @endsection@extends('_layouts.default'),@section('main'),@endsection
#在pubic 下新建 i/favicon.ico,css/custom.css
#修改custiom.css
.footer p { color: #7f8c8d; margin: 0; padding: 15px 0; text-align: center; background: #2d3e50;} .topbar-link-btn { color: #fff !important;}
#修改路由 app/Http/routes.php
//Route::get('/', 'WelcomeController@index'); //Route::get('home', 'HomeController@index'); Route::get('/',function(){ return View::make('index'); });
访问 sylblog.test.com/index.php 则可成功访问
#修改导航链接 _layout/nav.blade.php
<a href="{{ URL::to('login') }}" class="am-btn am-btn-primary am-topbar-btn am-btn-sm topbar-link-btn"><span class="am-icon-user"></span> Login</a>
#创建登录视图 login.blade.php
@extends('_layouts.default') @section('main') <div class="am-g am-g-fixed"> <div class="am-u-lg-6 am-u-md-8"> <br/> @if (Session::has('message')) <div class="am-alert am-alert-danger" data-am-alert> <p>{{ Session::get('message') }}</p> </div> @endif @if ($errors->has()) <div class="am-alert am-alert-danger" data-am-alert> <p>{{ $errors->first() }}</p> </div> @endif {!! Form::open(array('url' => 'login', 'class' => 'am-form')) !!} {!! Form::label('email', 'E-mail:') !!} {!! Form::email('email', Input::old('email')) !!} <br/> {!! Form::label('password', 'Password:') !!} {!! Form::password('password') !!} <br/> <label for="remember_me"> <input id="remember_me" name="remember_me" type="checkbox" value="1"> Remember Me </label> <br/> <div class="am-cf"> {!! Form::submit('Login', array('class' => 'am-btn am-btn-primary am-btn-sm am-fl')) !!} </div> {!! Form::close() !!} <br/> </div> </div> @stop
?有问题
#修改路由 app/Http/routes.php
<pre name="code" class="php">Route::post('login', array('before' => 'csrf', function(){ $rules = array( 'email' => 'required|email', 'password' => 'required|min:6', 'remember_me' => 'boolean', ); $validator = Validator::make(Input::all(), $rules); if ($validator->passes()) { if (Auth::attempt(array( 'email' => Input::get('email'), 'password' => Input::get('password'), 'block' => 0), (boolean) Input::get('remember_me'))) { return Redirect::intended('home'); } else { return Redirect::to('login')->withInput()->with('message', 'E-mail or password error'); } } else { return Redirect::to('login')->withInput()->withErrors($validator); } })); Route::get('home', array('before' => 'auth', function(){ return View::make('home'); }));
Route::get('logout', array('before' => 'auth', function(){ Auth::logout(); return Redirect::to('/'); }));