0

从昨天开始,我一直在尝试查看在 4.0.6 中工作的 Laravel 作曲家,但都击中了头部,我不知道该怎么想。

我遵循了 Laravel Brazil 的教程:Controllers with clean View Composer ( http://www.laravel.com.br/controllers-mais-limpos-com-view-composers/ )

你能帮助我吗?遵循以下所有代码(或部分代码):

作曲家.json

"autoload": {
  "classmap": [
    "app/commands",
    "app/controllers",
    "app/models",
    "app/macros",
    "app/services",
    "app/libraries",
    "app/viewComposers",
    "app/database/migrations",
    "app/database/seeds",
    "app/tests/TestCase.php"
  ]
},

应用程序/作曲家.php

<?php

View::composer('layouts.default', 'CategoryComposer');

应用程序/viewComposers/CategoryComposer.php

<?php 
class CategoryComposer {

  /**
   * compose method
   * Busca as categorias para a sidebar do front-end
   *
   * @access public
   * @param Array $view
   * @return Category
   * @since 1.0
   * @version 1.0
   * @author Patrick Maciel
   */
  public static function compose($view)
  {
    $categories = Category::getActive(false);
    $view->with('categories', $categories);

  }
}

应用程序/控制器/ProductsController.php

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function getIndex()
{

  $products = Product::getActive(false, false);

  return View::make('products.index')
    ->with('title', 'Produtos')
    ->with('products', $products);
}

应用程序/模型/Product.php

/**
 * getActive method
 * Obtém a lista de grupos ativos
 *
 * @access public
 * @return Array
 * @since 1.0
 * @version 1.0
 * @author Patrick Maciel
 */
public static function getActive($optional = true, $list = true)
{
  if ($optional AND $list) {

    return array('' => 'Selecione (opcional)') + static::active()->lists('name', 'id');

  } else if (!$optional AND $list) {

    return static::active()->lists('name', 'id');

  } else if (!$optional AND !$list) {

    return static::active()->get();

  }
}

应用程序/视图/产品/index.blade.php

@foreach ($categories as $category)
  <li class='categoria_produto'>
    <a href="{{ URL::to('/categoria/' . $category->id . '/' . Str::slug($category->name, '-')) }}" title="{{ $category->name }}">{{ $category->name }}</a>
  </li>
@endforeach

错误 当我尝试访问视图编写器发送的变量时产生错误。 - 类别作曲家

4

1 回答 1

0

Brayan Rastelli (brayanrastelli),为我解决问题:

我将视图作曲家的第一个参数(视图)更改为部分(适用于任何视图,布局除外 - 我不知道为什么)。

View::composer('partials.category_navigation', 'CategoryComposer');

而他们,我包括composers.phpfilters.php(最后几行):

/* ----------------------------------------------------------------------------
| Extra files
| -----------------------------------------------------------------------------
|
| View Composers, etc
|
*/
require_once app_path() . '/composers.php';

而且,在我看来,我@include认为:

@include('partials.category_navigation')

多谢你们。

于 2013-09-13T01:16:13.377 回答