0

i'm trying to retrieve the navigation bar data from the database using middleware class so i can access it anywhere within the application the data coming fine but the class printing the data as ajax response instead of passing it as an array when i tried to navigate any route within the application it's printing out the data and die and not showing the correct view

 <?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Response;
use App\Navigation;
use App\PrimaryNavigation ;
use Illuminate\Http\Request;
class NavigationMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $mainNavi = Navigation::with('PrimaryNavigation')->get() ;
        $subNavi = PrimaryNavigation::with('SubNavigation')->get() ;
        $navigation = [
            'main' => $mainNavi,
            'sub' =>$subNavi
        ];
        return Response($navigation);

    }
}

the middleware class registered within the kernel class

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \App\Http\Middleware\NavigationMiddleware::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

1 Answer 1

3

using middleware for doing that is not a good idea , use view composers or ... , see this topic too

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.