0

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);
        }

        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?

localheinz
  • 9,179
  • 2
  • 33
  • 44
Chad
  • 643
  • 2
  • 11
  • 22
  • 1
    Well technically you don't really call the middleware on the constructor.You just said, that the CheckRole middleware should be applied for all routes pointing to the Controller methods. Your example works fine for me if I call dd($request->all()) in a controller method like index. The request will not be modfied INSIDE the constructor. – shock_gone_wild Jul 21 '17 at 06:05

2 Answers2

1

Instead of this code line:

$request->merge(['group' => 123]);

You can try:

$request->request->add(['group' => 123]);

What this code line will do is if a parameter named group exists in the $request it will overwrite with the new value, otherwise it will add a new parameter group to the $request

In your controller, you can get the value of group parameter as:

$group = $request->group; OR $group = $request->input('group');

Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
  • Okay, when I do a dd($request) on the middleware page, I see it in the dump. However when I try to extract the value on the controller side, I get nothing. `echo "---->" . $request->get('group');` – Chad Jul 21 '17 at 06:08
  • I have edited my answer and now added the code to extract the value of the parameter in the controller. Let me know if you still have issues. – Rahul Gupta Jul 21 '17 at 06:12
  • Please consider accepting my answer and giving an UP if my code has really helped solving your issue. Thanks – Rahul Gupta Jul 21 '17 at 06:35
0

Thanks to the joint help of @Rahul-Gupta and @shock_gone_wild. It was a joint effort I guess.

The first issue is that I'm using sleepingOwl laravel boilerplate. Probably not the best idea for someone new to Laravel. (not new to MVC / PHP).

Based on @shock_gone_wild comment, decide move my test over to a simple controller, and not the sleeping owl nonsense. (they have a lot of code.) Anyways, I believe that helped. I did leave the middleware in the constructor because I didn't apply the middleware to the routes.

Then I followed @Rahul-Gupta syntax.

So here is final result, hopefully this will save someone sometime someday...

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) {


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

}

Then here is the simple controller.

use Illuminate\Http\Request;
use App\task;
use App\User;
use App\HasRoles;

class TaskController extends Controller {

public function __construct() {
  // constructor code...
  $this->middleware('auth');
  $this->middleware('CheckRole');
}

public function index(Request $request) {

   $group = $request->input('group');
   echo "---->" . $group;

   $tasks = Task::all();

   return view('test_task', compact('tasks'));

}

}
Chad
  • 643
  • 2
  • 11
  • 22