4

in my website i have a fairly complected category which i have to show in every view (in the client side) so i thought i put the code for creating category in a middleware and pass the result to views

so i've created my middleware but i cant figure out how can i pass its data to my view withouth having to do something in the controllers i've tried these methods in my middleware

<?php

namespace App\Http\Middleware;

use Closure;

class CtegoryMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->merge(array("all_categories" => "abc"));
        $request['all_categories']= 'abc';
        return $next($request);
    }
}

route :

Route::group(['middleware' => ['category' ]], function () {
     Route::get('/', 'HomeController@index');
});

but in my view when i echo all_categories i get

Undefined variable: all_categories 

btw i've checked by echoing something , the middleware gets triggered on the request

6
  • While the variable all_categories is available on your $request, perhaps you didn't pass it to your view within your controller?
    – JofryHS
    Oct 20, 2016 at 23:56
  • @JofryHS as i said i dont want to do anything in my controllers otherwise i would have created a helper class or something instead .... i just want to have specific data available in all my views (well most of them)
    – hretic
    Oct 21, 2016 at 0:01
  • Perhaps use View::share() to share your variables on your view? laravel.com/docs/5.3/views#sharing-data-with-all-views
    – JofryHS
    Oct 21, 2016 at 0:04
  • thanx this is better but there is no routing and i have to load all categories even in the admin section which i dont need .... i call this plan B
    – hretic
    Oct 21, 2016 at 0:16
  • Not entirely sure what 'there is no routing' means, but technically if you call view()->share() within your middleware, it should share the same variable on all subsequent views
    – JofryHS
    Oct 21, 2016 at 0:18

1 Answer 1

15

I think in your use case, using a globally available view variable should suffice.

<?php

namespace App\Http\Middleware;

use Closure;

class CtegoryMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $request->merge(array("all_categories" => "abc"));
        $request['all_categories']= 'abc';

        /**
         * This variable is available globally on all your views, and sub-views
         */
        view()->share('global_all_categories', 'abc');

        return $next($request);
    }
}

The variable is loaded once (if you do database query, the query will only execute once), and the variable is then stored in the View factory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.