Route::get(‘oauth/token’, function(){$query = http_build_query([‘client_id’=>config(‘client_id’),‘response_type’=>’code’,‘redirect_uri’=>url(‘oauth/callback’),]);return redirect(‘https://speakr.dev/oauth/authorize?’ . $query);});
路由将会将用户导向OAuth做事器认证页面,如果认证成功,则返回到redirect_uri页面,同时带回access_token以及refresh_token。个中access_token表示认证令牌,供应用户或运用程序的认证凭据。refresh_token表示刷新认证令牌,也便是须要更新上面的access_token时,须要利用的凭据。
当在界面认证完毕后,OAuth做事器会回调本地运用的页面,此时该当供应接口得到上面的token对:
Route::get('oauth/callback', function(Request $request){ $http = new GuzzleHttp\Client; $response = $http->post('https://speakr.dev/oauth/token', [form_params=>[ 'grant_type'=>'authorization_code', 'client_id'=>config('client_id'), 'client_secret'=>config('secret'), 'redirect_uri'=>url('oauth/callback'), 'code'=>$request->code, ]]); $this->UsersTokens = json_decode((string)$response->getBody(), true);});
上面便是调用OAuth认证做事器的模式,须要更新access_token时,改变下列信息即可:

$http->post('https://speakr.dev/oauth/token');'grant_type'=>'refresh_token','refresh_token'=>$the_old_refresh_token,
在调用后,同样会返回新的一组access_token和refresh_token。
如果前端javascript的调用也须要oauth的认证,可以设置一个叫做laravel_token的cookie,这个cookie的构造类似于JSON Web Token(JWT), 它由3个部分组成,第一部分是加密算法信息,第二部分是认证须要的实际内容,第三部分是对前两部分构造的解释。然后各个部分实现加密后用点号连接起来,末了再利用base64编码后通报。
完成认证后,作为管理员须要看到所有认证的用户,此时可以利用Vue组件来查看,通过在resources/assets/js/app.js中注册组件:
Vue.component( 'passport-clients', require('./components/passport/Clients.vue'));
当然,在此之前须要通过artisan导入组件。然后就可以以<passport-clients></passport-clients>在html中利用了。
在认证时,如果没有指定浸染域,则用户可以访问所有的内容(浸染域是)。可以在路由中指定认证的浸染域,起到类似授权的效果:
Route::get('/event', function(){ if(auth()->user()->tokenCan('add-delete-clips')){}});// 或者利用中间件的办法:Route::get('clips', function(){})->middleware('scopes:list-clips,add-delete-clips');
有两个中间件都可以利用,分别是scope和scopes, 差异在于scopes须要知足后面指定的所有浸染域,而scope只用知足个中一个。在利用中间件时,须要在$routeMiddlewar变量中添加配置,它位于app/Http/Kernel.php
要实现上面浸染域限定的功能,首先须要在AuthServiceProvider的boot()方法中定义:
public function boot(){ Passport::tokensCan([ 'list-clips'=>'List sound clips', 'add-delete-clips'=>'add new and delete...', 'admin-account'=>'Administer account...' ]);}
然后,用户在进行OAuth授权时,须要将权限项通报给OAuth做事器,以便在得到授权后,许可这些权限:
Route::get('oauth/callback', function(){ $query = http_build_query([ 'client_id'=>config('client_id'), 'redirect_uri'=>url('oauth/callback'), 'response_type'=>'code', 'scope'=>'list-clips add-delete-clips', ]); return redirect('http://speakr.dev/oauth/authorize' . $query);});
Laravel Passport是一个单独的包,能让你大略的在运用程序中创建和管理OAuth做事器。除了标准的认证办法外,laravel还供应了一个“api”的认证办法,利用时,须要在guard方法中显式声明: $user = auth()->guard('api')->user();利用api的认证办法,须要在user表中添加“api_token”字段,这是一个随机字段,可以用str_random(60)的办法添加。
Laravel可以支持多种文件形式存储,配置文件位于config/filesystems.php, 例如:
'disks'=>[ 'local'=>[ 'driver'=>'local', 'root'=>storage_path('app'), ] 'public'=>[ 'driver'=>'local', 'root'=>storage_path('app/public'), 'visibility'=>'public' ]]
storage_path('public')会返回路径storage/public。利用时,指定它的键即可:Storage::disk('s3')->get('file.jpg');除了get,还有其他一些方法:
put('file.jpg', $contentOrStream)pubFile('myDir', $file)exists('file.jpg')// 找出目录下的所有文件,返回一个数组files('myDir')
还可以添加其他的文件做事,比如Dropbox,首先须要建立一个DrBServiceProvider,然后在它的boot方法中实现,然后继续Storage类,返回FileSystem实例:
public function boot(){ Storage::extend('dropbox', function($app, $config){ $client = new DropboxClient( $config['accessToken'], $config['clientIdentifier'] ); return new Filesystem(new DropboxAdapter($client)); })}
通过$request->file()方法来得到一个SplFileInfo类,这个类供应了一个返回文件路径的getRealPath()方法,可以得到用户上传文件时的临时路径,然后再利用file_get_contents()方法就可以读取了。
将图像的宽度按比例调度到150,并且利用jpg格式存储,质量掌握为75, 可以利用下面的代码:
Image::make($original)->resize(150, null, function($constraint){ $constraint->aspectRatio();})->encode('jpg', 75);
访问session的办法可以多样化:
// 获取session()->get('user_id');$request->session()->get('user_id');session('user_id');// 设置session()->put('user_id', 'some_user_id');session(['user_id', 'some_user_id']);// 重新天生session_idsession()->regenerate();// 推举利用session()助手方法。// 同时也可以利用$request和$session变量,利用时,须要在路由中注入。Route::get('dashboard', function(Request $request){});Route::get('dashboard', function(Illuminate\Session\Store $session){});
cache的方法跟session的类似:
// 获取cache('key', 'default value');cache()->get('key', 'default value');// 保存cache(['key'=>'value'], $minutes);cache()->put('key', 'value', $minutes);// 查询一个值,如果没有就通过方法获取值,按照缓存韶光保存,并且返回值。cache()->remember($key, $minutes, $closure);// 删除缓存中的一个值。cache()->forget($key);
cookie的一些方法:
// 设置一个cookiecookie('some_key', true, 15);// 从要求工具中读取一个cookie$request->cookie('some_key', false);// 在相应中设置cookieResponse::view('dashboard')->cookie($cookie);
Laravel中的全文搜索叫做Scout,安装如下:composer require laravel/scout然后将ServiceProvider添加到配置文件config/app.php中: Laravel/Scout/ScoutServiceProvider::class通过命令来安装它的根本文件: php artisan vendor:publish末了将Algolia证书添加到config/scout.php。
可以对查询结果再次通过where子句查询:Review::search('Llew')->where('account_id', 2)->get();在config/scout.php中,将queue设为true就可以将这些更新设置为异步索引。如果想手工触发Eloquent的索引,则调用:Review:all()->searchable();如果想取消全文索引,则调用:Review::where('sucky', true)->unsearchable();
在测试时,如果须要检讨session中的键值是否很有缺点,可以传入一组键值:$this->assertSessionHasErrors['name', 'email'];
如果须要加密一个字符串,可以大略利用下面代码:app(Encrypter::class)->encrypt('something want to encrypt');测试一个cookie可以利用下面代码:$this->seeCookie('cookie-name');
Laravel的邮件模板通过php类来构建,须要继续Mailable类,实现build()方法:
class Assignment extends Mailable{ use Queueable, SerializesModels; public function build(){ // 指定邮件的主题和内容视图。 return $this->subject('New Assignment')->view('emails.assignment') // 个中,类的公共字段可以直接在模板中利用,也可以利用with方法来明确指定。 ->with(['assignment'=>$this->event->name]) // 利用底层工具来修正内置属性。 ->withSwiftMessage(function($swift){ $swift->setReplyTo('noreply@email.com'); }); } function send(){ // 用Mail工具来发送邮件。 Mail::to($user1) ->cc($user2) ->bcc($user3) ->send(new Assignment()); } function queue(){ // 把邮件加入行列步队,而不是立即发送。 Mail::queue(new Assignment()); } function test_mail(){ // 用于给发送的邮件进行判断。 Mail::assertSent(WelcomeEmail::class, function($email){ return $email->subject == 'Welcome!'; }); }}
如果须要测试发送效果,则在config/mail.php中作如下设置,以便覆盖每封邮件的to属性:'to'=>['address'=>'test@domain.com', 'name'=>'Testing Mail']
Laravel中的关照,继续自Notification类:
class WorkoutAvailable extends Notification{ use Queueable; // via方法用来定义利用哪些关照频道。 // $notifiable表示须要关照的工具,常日表示一个用户。 public function via($notifiable){ return ['mail']; } // 产生邮件信息给mail频道。 public function toMail($notifiable){ return new MailMessage(); } function notify(){ // 第一种发送办法,通过user主体来发起。 $user->notify(new WorkoutAvailable($workout)); // 第二种办法,通过Notification发送,可以发给多个user。 Notification::send($users, new WorkoutAvailable($workout)); }}
SMS关照通过Nexmo来发送,发送SMS前,须要先注册一个Nexmo账户。