composer create-project laravel/laravel larabbs --prefer-dist “9.*”
hosts文件
127.0.0.1 larabbs.test
DB_DATABASE=larabbs
APP_NAME=LaraBBS
APP_URL=http://larabbs.test
getenv(‘APP_NAME’)获取
env(‘APP_NAME’, ‘Laravel’)
config(‘app.timezone’)
{
...
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/helpers.php"
]
},
...
}
php artisan make:controller PagesController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function root()
{
return view('pages.root');
} }
Route::get(‘/’, ‘PagesController@root’)->name(‘root’);
laravel-8
取消protected $namespace = ‘App\Http\Controllers’;注释
laravel-9
...
protected $namespace = 'App\\Http\\Controllers';
public function boot()
{
...
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
...
}
php artisan migrate
laravel-9
users
add unique users_email_unique
(email
))use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
public function root(Request $request)
{
try {
$validated = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}catch (Exception $e){
return $e->getMessage();
}
}
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePostRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
}
public function messages()
{
return [
'title.required' => '标题不能为空',
'body.required' => '主体不能为空',
];
}
}
public function root(StorePostRequest $request)
{
}
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Throwable;
class Handler extends ExceptionHandler
{
...
public function render($request,Throwable $e)
{
if($e instanceof ValidationException){
$result = [
"code"=>400,
"msg"=>array_values($e->errors())[0][0],
];
return response()->json($result,400);
}
return false;
}
}
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
...
public function root(Request $request)
{
$validator = Validator::make($request->all(), [
'captcha' => ['required','min:4']
], [
'captcha.required' => '验证码不能为空',
'captcha.min' => '验证码不能少于4位'
]);
if ($validator->fails()) {
return array_values($validator->errors()->toArray())[0][0];
}
}
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Uppercase implements Rule
{
/**
* 确定验证规则是否通过。
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return strtoupper($value) === $value;
}
/**
* 获取验证错误消息。
*
* @return string
*/
public function message()
{
return ':attribute :attribute 需大写';
}
}
use App\Rules\Uppercase;
...
'name' => ['required', 'string', new Uppercase],
...