laravel new yourproject
cd yourproject
vim .env # 修改配置文件
以下内容来自Laravel-lang的github主页
For Laravel 5.8 : run composer require caouecs/laravel-lang:~4.0 in your project folder
For Laravel 5.1-7 : run in your project folder
For Laravel 5 : run composer require caouecs/laravel-lang:~2.0 in your project folder
For Laravel 4 : run composer require caouecs/laravel-lang:~1.0 in your project folder
Files of languages are in “vendor/caouecs/laravel-lang” directory
Copy the folders of languages that you want, in the resources/lang folder of your Laravel application (app/lang in Laravel 4).
我安装的是5.7,执行:
composer require caouecs/laravel-lang:~3.0
执行:
composer require itsgoingd/clockwork
对于5.5以前的版本还需要配置config/app.php文件:
'providers' => [
...
Clockwork\Support\Laravel\ClockworkServiceProvider::class
]
为什么使用clockwork而不是debugger,因为它的侵入性更低,而且debugger是运行在页面上的,如果刷新了或者打开多个页面的情况下,调试信息会分散,丢失。而clockwork配合chrome简直是神器,当然你也可以两个都安装。
安装laravel-cors是为了解决跨域问题,即使你的应用最终部署在一个服务器上,调试开发的时候前后端一般也是分别开发,同样存在跨域问题。
composer require barryvdh/laravel-cors
在app/Http/Kernel.php中进行全局配置:
protected $middleware = [
// ...
\Barryvdh\Cors\HandleCors::class,
];
进行局部配置:
protected $middlewareGroups = [
'web' => [
// ...
],
'api' => [
// ...
\Barryvdh\Cors\HandleCors::class,
],
];
关于这部分的内容,主要来自于:JWT 完整使用详解
composer require tymon/jwt-auth 1.*@rc
截止该博客编写时不加版本号默认安装的好像是0.5.12版本,和别的包有冲突,会发出错误提示。
php artisan vendor:publish --provider=“Tymon\JWTAuth\Providers\LaravelServiceProvider”
php artisan jwt:secret
主要是实现Tymon\JWTAuth\Contracts\JWTSubject这个接口:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
/**
* App\User
*
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
* @method static \Illuminate\Database\Eloquent\Builder|\App\User newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\User newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\User query()
* @mixin \Eloquent
*/
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt', // 原来是 token 改成jwt
'provider' => 'users',
],
],
这两个 Facade 并不是必须的,但是使用它们会给你的代码编写带来一点便利。
config/appp.php
'aliases' => [
...
// 添加以下两行
'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth',
'JWTFactory' => 'Tymon\JWTAuth\Facades\JWTFactory',
],
你可以在路由中使用jwt.auth这个中间件进行登录验证:
Route::post('register', 'AuthController@register');
Route::group([
'middleware' => 'jwt.auth'
], function ($router) {
Route::post('login', 'AuthController@login');
Route::post('logout', 'AuthController@logout');
});
一般使用JWT就是为了通过ajax的方式访问站点,我们封装ApiController作为所有控制器的父类,方便管理,。
<?php
namespace App\Http\Controllers;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
class ApiController extends Controller
{
public function failed($message = null, $code = FoundationResponse::HTTP_BAD_REQUEST, $status = 'error'){
return response()->json(['message' => $message], $code);
}
public function created($new = [])
{
return response()->json([
'created' => $new
],FoundationResponse::HTTP_CREATED);
}
/**
* @param $data
* @param string $status
* @return mixed
*/
public function success($data = []){
return response()->json($data, FoundationResponse::HTTP_OK);
}
public function getUserId(){
return auth()->user()->id;
}
public function user(){
return auth()->user();
}
}
在项目根目录下执行命令:
composer require “spatie/laravel-medialibrary:^7.0.0”
生成数据表迁移文件:
php artisan vendor:publish --provider=“Spatie\MediaLibrary\MediaLibraryServiceProvider” --tag=“migrations”
执行迁移:
php artisan migrate
生成配置文件:
php artisan vendor:publish --provider=“Spatie\MediaLibrary\MediaLibraryServiceProvider” --tag=“config”