Слишком мало аргументов для функции App \ Http \ Controllers \ Admin \ AccountsController :: index (), 0 передано и ровно 1 ожидается в laravel - PullRequest
0 голосов
/ 21 января 2020

Сессия уничтожается после успешного входа в систему или произошла ошибка с защитником, который не смог сохранить сеанс. Когда запрашивается your_session_key в представлении панели мониторинга, он предоставляет нулевое значение.

Route::group(['prefix' => 'admin'], function () {
Route::namespace('Admin')->group(function () {
Route::group(['middleware' => ['admin_middle','auth:admin']] , function () {
        Route::get('accounts/', 'AccountsController@index')->name('admin.accounts');
        });
    });
});

Middleware: App \ Http \ Middleware \ RedirectIfNotAdmin // Зарегистрирован в ядре как 'admin_middle' => \ App \ Http \ Middleware \ RedirectIfNotAdmin :: class,

class RedirectIfNotAdmin
{
public function handle($request, Closure $next, $guard = 'admin')
{
    if (!auth()->guard($guard)->check()) {
        $request->session()->flash('error', 'You must be an Admin to see this page');
        return redirect(route('auth.admin.login'));
    }

    return $next($request);
}
}

Guard: config / auth. php // Custom Guard

'guards' => [
'admin' => [
    'driver' => 'session',
    'provider' => 'admin',

],
],

AccountsController: Контроллеры \ AccountsController

class AccountsController расширяет контроллер {

public function __construct(AdminRepositoryInterface $adminRepository) {
    $this->adminRepo = $adminRepository;
}

private $adminRepo;
public function index(int $id)
{
    $admin = $this->adminRepo->findAdminById($id);

    $talentRepo = new AdminRepository($admin);


    return view('admin.accounts');
}

}

AdminRepositoryInterface: App \ Shop \ Admins \ Repositories \ Interfaces \ AdminRepositoryInterface;

interface AdminRepositoryInterface extends BaseRepositoryInterface
{
public function findAdminById(int $id) : Admin;
}

AdminRepository: App \ Shop \ Admins \ Репозитории \ AdminRepository

class AdminRepository extends BaseRepository implements AdminRepositoryInterface
{
public function findAdminById(int $id) : Admin
{
    try {
        return $this->findOneOrFail($id);
    } catch (ModelNotFoundException $e) {
        throw new AdminNotFoundException($e);
    }
}
}

Просмотр: admin \ accounts.blade

@if (Session::has('YOUR_SESSION_KEY'))
{{-- do something with session key --}} 
@else
{{-- session key does not exist  --}} //this has been printed is the ID variable is not passed
@endif
{{$admin->name}}
 <br />{{$admin->email}}

1 Ответ

1 голос
/ 21 января 2020

Это говорит, что $this->adminRepo возвращает null.

Попробуйте инициализировать $this->adminRepo в конструкторе контроллера. Если вы напишите интерфейс, убедитесь, что вы связали его с поставщиком услуг.

https://laravel.com/docs/5.8/container#binding -основы

...