您知道如何将此代码从视图正确移动到控制器吗?

时间:2018-06-07 22:11:16

标签: php laravel

我在下面的代码中显示了以下注册摘要:

Registration type        Quantity            Price           Subtotal
general                     1                0.00             0.00
plus                        2                2.00             4.00

视图中的代码显示摘要:

<ul>
    <li>
        <span>Registration Type</span>
        <span>Quantity</span>
        <span >Price</span>
        <span>Subtotal</span>
    </li>

    <?php
    $total = 0;
    $type_counts = [];
    foreach ($registrationTypeDetails->participants as $p) {
        $name = $p->registration_type->name;
        if (!isset($type_counts[$name])) {
            $type_counts[$name] = 0;
        }
        $type_counts[$name]++;
    }
    ?>
    @foreach($registrationTypeDetails->participants as $participant)
        @php ($name = $participant->registration_type->name)
        @php ($total +=$participant->registration_type->price * $type_counts[$name])
        <li>
            <span>{{$name}}</span>
            <span>{{$type_counts[$name]}}</span>
            <span>{{ number_format($participant->registration_type->price, 2)}}$</span>
            <span>{{ number_format($participant->registration_type->price * $type_counts[$name], 2)}}$</span>
        </li>
    @endforeach
    <li>
        <span>TOTAL</span>
        <span>{{ number_format($total, 2)}}€</span>
    </li>
</ul>

它工作正常,但我想将视图中的上面的php代码传递给控制器​​showSummary()方法,然后返回视图所需的数据来显示摘要,但我没有成功实现这一目标。

你知道如何正确实现这一目标吗?

我在控制器中有showSummary()方法,它将“registrationTypeDetails”返回到上面的视图。

public function showSummary($id, $slug, $regID)
    {
        $registrationTypeDetails = Registration::with(['participants.registration_type',
            'participants' => function ($query) use ($regID) {
                $query->select('id', 'registration_type_id', 'registration_id')->where('registration_id', $regID);
            }
        ])->find($regID);

        return view('conferences.showSummary', compact('registrationTypeDetails', 'id', 'slug'));
    }

我为实现这一目的所做的是在控制器中放入计算每种注册类型数量的部分,如:

public function payment($id, $slug, $regID)
    {
        $registrationTypeDetails = Registration::with(['participants.registration_type',
            'participants' => function ($query) use ($regID) {
                $query->select('id', 'registration_type_id', 'registration_id')->where('registration_id', $regID);
            }
        ])->find($regID);

        $type_counts = [];
        foreach ($registrationTypeDetails->participants as $p) {
            $name = $p->registration_type->name;
            if (!isset($type_counts[$name])) {
                $type_counts[$name] = 0;
            }
            $type_counts[$name]++;
        }

        $registrationTypes = [];

        return view('conferences.payment', compact( 'type_counts', 'registrationTypeDetails', 'id', 'slug'));
    }

在视图中:

<ul>
    <li>
        <span>Registration Type</span>
        <span>Quantity</span>
        <span>Price</span>
        <span>Subtotal</span>
    </li>
    <?php
    $total = 0;
    ?>
    @foreach($registrationTypeDetails->participants as $participant)
        @php ($name = $participant->registration_type->name)
        @php ($total +=$participant->registration_type->price * $type_counts[$name])
        <li>
            <span>{{$name}}</span>
            <span>{{$type_counts[$name]}}</span>
            <span>{{ number_format($participant->registration_type->price, 2)}}
                €</span>
            <span>{{ number_format($participant->registration_type->price * $type_counts[$name], 2)}}
                €</span>
        </li>
    @endforeach
    <li>
        <span>TOTAL</span>
        <span>{{ number_format($total, 2)}}€</span>
    </li>
</ul>

但是,如果有更多的代码应该发送到控制器,我不知道这是否可以。因为我需要在会话中存储注册的总值($total),但总值($ total)在视图中不在控制器中,这看起来不正确。

1 个答案:

答案 0 :(得分:1)

答案都不是。

控制器用于将数据从一个地方传递到另一个地方,而视图用于显示数据。一个好的规则是,如果你正在编写PHP并在Blade模板中创建数组,那么你做的太多了(听起来你已经知道了)。

在您的应用中创建一个名为repositories的文件夹,然后创建一个RegistrationRepository或类似内容。将您的表格构建为Collection - 或者以更有效的方式从Eloquent获取。然后使用Collection map()创建总计。将该数据传递到视图中,然后简单地遍历它并显示它。

让你的模型和控制器保持轻量级,创建新的类以保持复杂的逻辑。还有一个论点,这可以作为View Composer完成,但我认为你正在创建额外的信息并从中读取很少的地方,为它创建一个存储库是最干净的。

然而这是一个意见,而不是答案。遗憾的是没有明确的答案,因为MVC是你真正想要的。 Rails喜欢沉重的模型,CodeIgniter喜欢沉重的控制器。 我个人我相信Laravel是关于拥有灯光控制器和灯光模型 - 但是由你决定是否同意。

相关问题