Laravel and Ratchet 建立一个简单博客

PHP Laravel WebSocket

总结一些案例的主要步骤。

Hello world 主页案例


 $ php artisan controller:make WelcomeController --only=index 
    return View::make('welcome.index');
Route::get('/', function()
{
    return View::make('hello');
});

RESTful案例


Route::resource('articles','ArticlesController');
 $ php artisan controller:make ArticleController 
class Article extends Eloquent
{ 
    protected $fillable = array('title', 'text');
}
 $ php artisan migrate:make create_articles_table --create=articles 
 $ php artisan migrate 
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'));//显示
}

Authentication 案例


$ 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();
});
$ 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
php artisan generate:model article
php artisan generate:model page
 $ 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::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 

Reference


Laravel 入门

Laravel 4 系列入门教程