jeudi 20 juillet 2017

Laravel 5.4: Passing a variable via Request to controller

Generally speaking this should be a rather simple problem. IT should be very similar to the following question on Stack Overflow

But seeing as it has been two years, maybe some of the syntax has changed.

All I want to do is pass a variable from the middleware to the controller, so I'm not duplicating mysql queries.

Here is my middleware:

namespace App\Http\Middleware;

use Closure;

class CheckRole {

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next) {
$id = $request->user()->id;
$rr = $request->user()->isSuperAdmin();
if ($request->user()->isSuperAdmin()) {

    $request->merge(['group' => 123]);
    return $next($request);
} else {
    echo "not admin";
}
}

}

So the middleware works fine and if I DD($request) on the middleware I see my group => 123 on the page. (Right now it's 123 for the sake of simplicity.)

So I want to pass it to my AdminController:

<?php

namespace SleepingOwl\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use SleepingOwl\Admin\Form\FormElements;
use SleepingOwl\Admin\Form\Columns\Column;
use SleepingOwl\Admin\Display\DisplayTable;
use Illuminate\Contracts\Support\Renderable;
use SleepingOwl\Admin\Display\DisplayTabbed;
use Illuminate\Validation\ValidationException;
use SleepingOwl\Admin\Contracts\AdminInterface;
use SleepingOwl\Admin\Model\ModelConfiguration;
use Illuminate\Contracts\Foundation\Application;
use SleepingOwl\Admin\Contracts\Form\FormInterface;
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface;

class AdminController extends Controller {

/**
 * @var \DaveJamesMiller\Breadcrumbs\Manager
 */
protected $breadcrumbs;

/**
 * @var AdminInterface
 */
protected $admin;

/**
 * @var
 */
private $parentBreadcrumb = 'home';

/**
 * @var Application
 */
public $app;

/**
 * AdminController constructor.
 *
 * @param Request $request
 * @param AdminInterface $admin
 * @param Application $application
 */
public function __construct(Request $request, AdminInterface $admin,             Application $application) {
$this->middleware('CheckRole');

So as you can see I call the middleware on this constructor. After calling it I should be able do something like:

$request->get('group'); or $request->group;

After trying for quite a while nothing seems to be working and I keep getting a null value. Fundamentally, this shouldn't be terribly difficult, but I seem to have my syntax off or not using the right name spaces?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2vq1Tev
via IFTTT

Aucun commentaire:

Enregistrer un commentaire