Search code examples
phplaravellaravel-4renderpartial

Laravel render\call controller action from view\layout


Maybe someone can tell me how to use something like embedded controllers in symfony2, to call\render controller action in Laravel4,5?


Solution

  • Found the best way for me, for L5:

    CartServerProvider

    use Illuminate\Support\ServiceProvider;
    
    class CartServiceProvider extends ServiceProvider {
    
      /**
       * Register the service provider.
       *
       * @return void
       */
      public function register()
      {
        $this->app->make('view')->composer('layouts.master', 'Vendor\Cart\Http\ViewComposers\CartComposer');
      }
    
    }
    

    The CartComposer class looks like this:

    use Illuminate\Contracts\View\View;
    
    class CartComposer {
    
      /**
       * Cart manager instance.
       *
       * @var \Vendor\Cart\StoreInterface
       */
      protected $cart;
    
      /**
       * Create a new CartComposer instance.
       */
      public function __construct()
      {
        $this->cart = app()->make('cart.store');
      }
    
      /**
       * Compose the view.
       *
       * @return void
       */
      public function compose(View $view)
      {
        $view->with('cart', $this->cart);
      }
    
    }
    

    cart.store is a custom cart implementation I injected into the container, but the above should be enough to show you how to register a simple view composer.