第一个问题
<code>Route::get('test', function () { session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });</code>
输出1,然后我把session(['wechat.oauth_user'=>1]);
注释了
<code>Route::get('test', function () { //session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });</code>
刷新页面,输出null,这是为什么?
第二个问题
我使用了overtrue/wechat的插件,用插件自带的中间件成功获取了用户信息,然后user路由里也输出了session
<code> public function handle($request, Closure $next, $guard = null) { $wechat = app('EasyWeChat\\Foundation\\Application'); if (!session('wechat.oauth_user')) { if ($request->has('state') && $request->has('code')) { session(['wechat.oauth_user' => $wechat->oauth->user()]); return redirect()->to(url($request->url(), array_except($request->query(), ['code', 'state']))); } return $wechat->oauth->redirect($request->fullUrl()); } return $next($request); }</code>
<code> Route::get('user', function () { dd(session('wechat.oauth_user')); });</code>
在这个基础上,我建立了自己的中间件UserMiddleware
<code> public function handle($request, Closure $next) { if(Auth::guard('web')->guest()){ if (!session('wechat.oauth_user')) { return redirect('user'); } else{ User::CreateOrLogin(session('wechat.oauth_user')); } } return $next($request); }</code>
User.php
方法 CreateOrLogin
是
<code> public static function CreateOrLogin($data){ $user=self::getByOpenid($data['id']); if(!$user){ $create=['openid'=>$data['id']]; $user=self::create($create); } Auth::guard('web')->login($user); } public static function getByOpenid($openid){ return self::where('openid','=',$openid)->first(); }</code>
一切都是ok的,但是每次刷新页面都会执行微信获取用户信息的接口,然后一个个注释掉,发现Auth::guard('web')->login($user);
这句注释掉后,刷新页面就不会再跳转到微信,加上去就要跳转到微信获取用户信息接口,感觉像是login会清除掉session一样
问题三
<code>Route::group(['middleware' => ['web']], function () { Route::get('test', function () { dd(auth()->user()); }); Route::get('test2', function () { dd(auth()->loginUsingId(1)); }); });</code>
先访问test2,成功登陆,再访问test,输出null,为什么。。。
问题一和问题二和问题三没有关联,求大神解答
回复内容:
第一个问题
<code>Route::get('test', function () { session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });</code>
输出1,然后我把session(['wechat.oauth_user'=>1]);
注释了
<code>Route::get('test', function () { //session(['wechat.oauth_user'=>1]); dd(session('wechat.oauth_user')); });</code>
刷新页面,输出null,这是为什么?
第二个问题
我使用了overtrue/wechat的插件,用插件自带的中间件成功获取了用户信息,然后user路由里也输出了session
<code> public function handle($request, Closure $next, $guard = null) { $wechat = app('EasyWeChat\\Foundation\\Application'); if (!session('wechat.oauth_user')) { if ($request->has('state') && $request->has('code')) { session(['wechat.oauth_user' => $wechat->oauth->user()]); return redirect()->to(url($request->url(), array_except($request->query(), ['code', 'state']))); } return $wechat->oauth->redirect($request->fullUrl()); } return $next($request); }</code>
<code> Route::get('user', function () { dd(session('wechat.oauth_user')); });</code>
在这个基础上,我建立了自己的中间件UserMiddleware
<code> public function handle($request, Closure $next) { if(Auth::guard('web')->guest()){ if (!session('wechat.oauth_user')) { return redirect('user'); } else{ User::CreateOrLogin(session('wechat.oauth_user')); } } return $next($request); }</code>
User.php
方法 CreateOrLogin
是
<code> public static function CreateOrLogin($data){ $user=self::getByOpenid($data['id']); if(!$user){ $create=['openid'=>$data['id']]; $user=self::create($create); } Auth::guard('web')->login($user); } public static function getByOpenid($openid){ return self::where('openid','=',$openid)->first(); }</code>
一切都是ok的,但是每次刷新页面都会执行微信获取用户信息的接口,然后一个个注释掉,发现Auth::guard('web')->login($user);
这句注释掉后,刷新页面就不会再跳转到微信,加上去就要跳转到微信获取用户信息接口,感觉像是login会清除掉session一样
问题三
<code>Route::group(['middleware' => ['web']], function () { Route::get('test', function () { dd(auth()->user()); }); Route::get('test2', function () { dd(auth()->loginUsingId(1)); }); });</code>
先访问test2,成功登陆,再访问test,输出null,为什么。。。
问题一和问题二和问题三没有关联,求大神解答
检查是否加载了 web
中间件

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
