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 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?