douyin8809 2019-02-26 18:59
浏览 89
已采纳

从外部脚本检查Laravel 5.7登录不使用POST

I'm having exactly the same problem as Álvaro did here:

Check Laravel 5.7 login from external script

I had an external script I've not used since upgrading to 5.7 with the cookies encryption and now it's not working on POST requests, just GET.

I've tried the Route::any('/'... trick in his own answer, however, I can't see where he's getting the $response variable from in his sample code either - and I've tried disabling both:

\App\Http\Middleware\EncryptCookies::class,
\App\Http\Middleware\VerifyCsrfToken::class,

from the Http\Kernel

In regards to my code - it's pretty much exactly the same as his - and isn't working:

require_once __DIR__.'/../../../vendor/autoload.php';

$app = require_once __DIR__.'/../../../bootstrap/app.php';

$app->make('Illuminate\Contracts\Http\Kernel')
    ->handle(Illuminate\Http\Request::capture());

$isAuthorized = Auth::check();
if(!$isAuthorized){
    echo "Not Authorized";
    exit();
}

Seemed to have hit a dead end with this one so any advice towards a fix or even the mysterious $response would be greatly appreciated!

Edit/Update: Interestingly, I have a Route::fallback() within my routes/web.php and removing that causes auth to fail on the external GET request too, so it may actually be associated with Routing and it not being loaded from within the Laravel app router provider?

  • 写回答

1条回答 默认 最新

  • douyaju4749 2019-02-26 19:44
    关注

    Got it! After such a long time working on this, maybe working through writing the question above led me to a clearer frame of mind in actually looking into this.

    To investigate, something I did was stick this in my external PHP file, just below:

    require_once __DIR__.'/../../../vendor/autoload.php';
    
    $app = require_once __DIR__.'/../../../bootstrap/app.php';
    
    $app->make('Illuminate\Contracts\Http\Kernel')
        ->handle(Illuminate\Http\Request::capture());
    

    I added this:

    echo json_encode(Route::current()->parameters());
    

    This simply guided me to see which route declaration was catching the request, and it was the fallback route - which doesn't match on POST.

    What made my situation (and therefore resolution) different to the original requestor / question was that my 'addons' folder was within my laravel Public folder. This meant it wasn't catching a generic / route path - but the actual addons/file.php as the route parameter.

    Therefore my resolution was simply adding this to the bottom of my routes web.php file - just before the fallback route:

    Route::any('addons/file.php', null);
    

    All sorted - now the addons are loaded and authenticated fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)