Laravel View Composer无法正常工作

我创建了以下View Composer:</ p>

Http \ Providers \ ComposerServiceProvider </ p>

 &lt;?php 

namespace App \ Providers;

use Illuminate \ Support \ ServiceProvider;

class ComposerServiceProvider扩展ServiceProvider
{
/ **
*引导应用程序服务。

  • @return void
  • /
    公共函数boot()
    {
    view() - &gt; composer(
    'laysts.cart' ,
    'App \ Http \ ViewComposers \ CartComposer'
    );
    }

    / **
    *注册应用程序服务。

  • @return void

  • /
    public 函数寄存器()
    {
    //
    }
    }
    </ code> </ pre>

    Http \ ViewComposers \ CartComposer </ p>

      &lt;?php 

namespace App \ Http \ ViewComposers;

use Iluminate \ View \ View;
use App \ Order;

class CartComposer
{
public cartDetails = null;

公共函数__construct()
{
// dd('here1');
$ orderToken = session('o_token');
$ this-&gt; cartDetails = Order :: getOrderDetails($ orderToken);
}

公共函数撰写(查看$ view)
{
// // dd('here2');
$ view-&gt; with('cart',['total_items'=&gt; 7]);
}
}
</ code> </ pre>

为了测试,我正在返回一个硬编码数组['total_items'=&gt; 7] </ p>

现在我的视图通过@include包含在我的header.blade.php中:</ p>

views \ layouts \ cart.blade。 php </ p>

 &lt; div class =“cart-menu”&gt; 
&lt; i class =“black large cart icon”&gt;&lt; / i&gt;
@ if(/ * isset($ cart-&gt; total_items)&amp;&amp; * / $ cart-&gt; total_items&gt; 0)
&lt; div class =“floating ui red label cart-items-badge”&gt; { {$ cart-&gt; total_items}}&lt; / div&gt;
@endif
&lt; / div&gt;
</ code> </ pre>

我通过添加它来注册它 providers数组:</ p>


App \ Providers \ ComposerServiceProvider :: class,</ p>
</ blockquote>

当我访问我的时 页面我收到'页面无响应'错误。 我甚至看不到Laravel的错误。</ p>

有什么建议吗? 谢谢</ p>
</ div>

展开原文

原文

I've created the following View Composer:

Http\Providers\ComposerServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer(
            'layouts.cart',
            'App\Http\ViewComposers\CartComposer'
        );
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Http\ViewComposers\CartComposer

<?php

namespace App\Http\ViewComposers;

use Iluminate\View\View;
use App\Order;

class CartComposer
{
    public cartDetails = null;

    public function __construct()
    {
        //dd('here1');
        $orderToken = session('o_token');
        $this->cartDetails = Order::getOrderDetails($orderToken);
    }

    public function compose(View $view)
    {
        //dd('here2');
        $view->with('cart', ['total_items' => 7]);
    }
}

Just for the sake of testing, I'm returning a hardcoded array ['total_items' => 7]

And now my view which is included via @include in my header.blade.php:

views\layouts\cart.blade.php

<div class="cart-menu">
  <i class="black large cart icon"></i>
  @if (/*isset($cart->total_items) &&*/ $cart->total_items > 0)
  <div class="floating ui red label cart-items-badge">{{ $cart->total_items }}</div>
  @endif
</div>

I've registered it by adding it to the providers array:

App\Providers\ComposerServiceProvider::class,

When I access my page I'm getting a 'Page is Not Responding' error. I can't even see the Laravel error.

Any suggestion? Thanks

doulianxi0587
doulianxi0587 确实,查看/storage/logs帮助我找出问题所在。谢谢你的建议。有没有办法在屏幕上“激活”错误日志?
大约 2 年之前 回复
dongpao1926
dongpao1926 检查错误日志?查看/storage/logs以及您的Web服务器日志所在的位置。
2 年多之前 回复

1个回答



使用此ComposerServiceProvider文件</ p>

 命名空间App \ Providers; 

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

class ComposerServiceProvider扩展ServiceProvider
{
/ **
*引导应用程序服务。

  • @return void
  • /
    public function boot()
    {
    View :: composer(
    'layouts.cart',
    'App \ Http \ ViewComposers \ CartComposer'
    );
    }

    / * *
    *注册应用程序服务。

  • @return void

  • /
    公共函数寄存器()
    {
    //
    }
    }
    </ code> </ pre>

    views \ layouts \ cart.blade.php使用$ cart ['total_items']而不是$ cart-&gt; total_items </ p>

     &lt;  div class =“cart-menu”&gt; 
    &lt; i class =“black large cart icon”&gt;&lt; / i&gt;
    @if($ cart ['total_items']&gt; 0)
    &lt; div class =“floating ui red label cart-items-badge”&gt; {{$ cart ['total_items' ]}}&lt; / div&gt;
    @endif
    &lt; / div&gt;
    </ code> </ pre>
    </ div>

展开原文

原文

Use this ComposerServiceProvider file

namespace App\Providers;

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

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer(
            'layouts.cart',
            'App\Http\ViewComposers\CartComposer'
        );
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

views\layouts\cart.blade.php use $cart['total_items'] instead of $cart->total_items

<div class="cart-menu">
  <i class="black large cart icon"></i>
  @if ($cart['total_items'] > 0)
  <div class="floating ui red label cart-items-badge">{{ $cart['total_items'] }}</div>
  @endif
</div>

doudie2693
doudie2693 不,我仍然得到同样的错误。 顺便说一句,我认为您的服务提供商和我的服务提供商之间没有任何区别。 我正在使用帮助器时使用View外观,我认为这不会有所作为,我错了吗?
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问