lundi 19 septembre 2016

in Laravel how to change the view content at runtime each time before compilation

in Laravel 5 I need to amend a view content at runtime before compilation by adding this string: "@extends(foo)"

note: changing the view file content is not an option

so the process will be something like (each time a view is called)

  1. getting the view content
  2. edit the view content by appending "@extends(foo)" keyword
  3. compile (render) the view

I have tried using viewcomposer and middleware with no luck

here is my composer service provider:

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    public function boot()
    {

        View::composer('pages/*', function ($view) {

             // i want to do the following:
             // 1- find all view under directory resources/views/pages
             // 2- then add the following blade command "@extends(foo)" at the beginning of the view before compile

        });

    }


    public function register()
    {
        //
    }
}

and here is my view middleware try (in middleware i was able to amend view content after compilation :( )

<?php
namespace App\Http\Middleware;
use Closure;
class ViewMiddleware
{
    public function handle($request, Closure $next)
    {
        $response =  $next($request);
        if (!method_exists($response,'content')) {
            return $response;
        }

        $content  = "@extends('layouts.app')".$response->content();
        $response->setContent($content);
        return $response;
    }
}

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire