Доступ к $app или $request в Eloquent с помощью Slim Framework

Я создаю приложение Slim Framework 4 Api с Eloquent.

public/index.php

$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($dbconfig);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$app->add(new RequestUser()); // for calling middleware that adds user

Промежуточное ПО

 class RequestUser
  {

public function __invoke(Request $request, RequestHandler $handler): Response
{
     ....................
    $user = User::where('emailid', $email)->first();
    $request = $request->withAttribute('user', $user);
    $request = $request->withAttribute('token', (string) $exploded_authorization[1]);
    return $handler->handle($request);
  }
  }

Контроллер

use Psr\Http\Message\ServerRequestInterface as Request;

public function create(Request $request, Response $response, $args)
    {
$user = $request->getAttribute('user'); // gives me the request user information

    }

Модель

<?php

namespace App\Models\TableModel;

use Illuminate\Database\Eloquent\Model;
use DateTime;
use Psr\Http\Message\ServerRequestInterface as Request;


class Details extends Model
{
    protected $table = 'my_table';

    protected $primaryKey = 'Id';

    public $timestamps = false;

    protected $fillable = [];

    protected $hidden = [];

    protected $casts = [

    ];

    protected $appends = ['can_edit', 'can_delete'];


    public function getCanEditAttribute(){
        $now = date('Y-m-d H:i:s');
        return $this->Start_Date >= $now;
    }

    public function getCanDeleteAttribute(){
        //request contains the user information from the middleware? How to access here
        return 
    }


}

Я хочу получить доступ к запросу $ в моей модели, чтобы я мог получить информацию о пользователе, который пытается получить доступ к CanDelete.


person Alaksandar Jesus Gene    schedule 12.03.2020    source источник


Ответы (1)


У вас неправильное промежуточное программное обеспечение, оно должно быть промежуточным программным обеспечением маршрутизации и напрямую привязано к маршруту или группе маршрутов, а не к $app-> add

person Shuyi    schedule 08.05.2020
comment
Как привязать его к маршруту - person Alaksandar Jesus Gene; 08.05.2020
comment
slimframework.com/docs/v4/objects/routing.html# route-middleware , здесь все сказано... Промежуточное ПО Slim 4 немного сбивает с толку... - person Shuyi; 08.05.2020