[Laravel] 從closure 到controller,如何用php artisan建立controller檔

1、如何建立controller

// 這會在app/controllers資料夾裡建立一個UsersController.php這個檔案
# php artisan controller:make UsersController
// 也可以指定存放位置:以下這會在app/controllers/another_directory資料夾裡建立UsersController.php這個檔案(another_directory必須已先建立。)
# php artisan controller:make --path="app/controllers/another_directory" UsersController

2、從route.php的程式碼,移到controller檔:

// app/routes.php
Route::get("users", "UsersController@index");
Route::get("users/{username}", "UsersController@show");

// app/controllers/UsersController.php
<?php
class UsersController extends \BaseController {
  public function index()
  {
    $users = User::all();
    //return View::make("users.index")->with("users", $users);
    //return View::make("users.index")->withUsers($users);
    return View::make("users/index", ["users" => $users]);
  }
  
  public function show($username){
    $user = User::whereUsername($username)->first();  // select * from users where username = USERNAME LIMIT 1
    return View::make("users.show", ["user" => $user]);
  }
}