Laravel从view \ layout渲染\调用控制器动作

时间:2014-11-29 18:04:08

标签: php laravel laravel-4 render partial

也许有人可以告诉我如何在symfony2中使用类似嵌入式控制器的东西来调用Laravel4,5中的\ render controller动作?

1 个答案:

答案 0 :(得分:0)

找到最适合我的方式,对于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');
  }

}

CartComposer 类如下所示:     

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 是我注入容器的自定义购物车实现,但上面应该足以向您展示如何注册一个简单的视图编辑器。