总结一些案例的主要步骤。
Controller
: php artisan 生成WelcomeController $ php artisan controller:make WelcomeController --only=index
Controller
: 修改app/controllers/WelcomeController 中的index方法, return View::make('welcome.index');
View
: app/view/welcome/index.blade.php 填写对应视图HTML
route
: app/routes.php中修改对应的路由
Route::get('/', function()
{
return View::make('hello');
});
route
: app/routes.phpz中加入下面一句话,生成RESTful控制器,articles对应ArticlesControllerRoute::resource('articles','ArticlesController');
Controller
: php artisan 生成ArticlesController, 用以处理文章的CRUD(Create, Read, Update, Delete) $ php artisan controller:make ArticleController
Controller
:修改ArticlesController.php的create动作,return View::make(‘article.create’);
View
:app/view/article/create.blade.php 填写对应视图HTML,可用Form::表单构造器。为了让post转到/articles,修改Form::open(array('url'=>'articles'))
,对应于ArticlesController的store动作。
Model
: app/models/Article.php
class Article extends Eloquent
{
protected $fillable = array('title', 'text');
}
Model
: 准备迁移数据库,迁移文件在app/database/migrateions目录下,文件名2015_01_12_190254_create_articles_table.php $ php artisan migrate:make create_articles_table --create=articles
Model
: 然后修改2015_01_12_190254_create_articles_table.php文件中的up(),加入column的名字
Model
: 真正迁移数据库,完成数据库的创建
$ php artisan migrate
Controller
,route
: 修改ArticlesController中的store()存储文章,和show()显示文章public function store()
{
$article = Article::create(
array('title'=>Input::get('title'), 'text'=>Input::get('text'))
); // 使用Article model保存文章
return Redirect::route('articles.show', array($article->id)); // 交给app/routes去找articles.show,也就是Article Controller中的show($id)方法
}
public function show($id)
{
$article = Article::find($id);//查找文章
return View::make('articles.show',compact('article'));//显示
}
View
:app/view/articles/show.blade.php 显示 { { $article->title } } { { $article->text} }Database
:用migrate生成数据库,位于/app/database/migrate,修改其up()方法。$ php artisan migrate:make create_articles_table --create=articles
$ php artisan migrate:make create_pages_table --create=pages
// 修改up()方法
$ php artisan migrate
Schema::create('articles', function(Blueprint $table)
{
$table->increments('id');
$table->string('title');
$table->string('slug')->nullable();
$table->text('body')->nullable();
$table->string('image')->nullable();
$table->integer('user_id');
$table->timestamps();
});
Database
:用seed生成继承Seeder的类,位于/app/database/seed,然后修改其中的run方法。$ php artisan generate:seed page//用seed生成继承Seeder的类
$ php artisan generate:seed article//用seed生成继承Seeder的类
// 修改run方法
$ php artisan generate:seed Sentry//用seed生成继承Seeder的类
//...
$ php artisan db:seed//真正的seed
Model
:php artisan generate:model article
php artisan generate:model page
app/controllers/admin/AuthController.php
using $ php artisan generate:controller admin/AuthControler
void postLogin() {
$credentials = array('email' => Input::get('email'), 'passwords' => Input::get('password'));
try{
$user = Sentry::authenticate($credentials, false);
if($user){
return Redirect::route('admin.pages.index');
}
}catch(\Exception $e){
return Redirect::route('admin.login')->withErrors(array('login'=>$e->getMesssage()));
}
}
Route
:Route::get('admin/logout', array('as' => 'admin.logout', 'uses' => 'App\Controllers\Admin\AuthController@getLogout'));
Route::get('admin/login', array('as' => 'admin.login', 'uses' => 'App\Controllers\Admin\AuthController@getLogin'));
Route::post('admin/login', array('as' => 'admin.login.post', 'uses' => 'App\Controllers\Admin\AuthController@postLogin'));
Route::group(array('prefix' => 'admin', 'before' => 'auth.admin'), function()
{
Route::any('/', 'App\Controllers\Admin\PagesController@index');
Route::resource('articles', 'App\Controllers\Admin\ArticlesController');
Route::resource('pages', 'App\Controllers\Admin\PagesController');
});
check via:
$ php artisan routes