mercredi 10 février 2016

Optionally pass through a single view parameter in Laravel 5

Consider this scenario where I want to SOMETIMES pass data through to my view:

public function show($orderId)
{
    if ($sometimesTrue == true)
    {
        $optionalParameter = 'optional parameter';
    }

    return view('show', compact([$a, $b, $c, $optionalParameter]));
}

$optionalParameter is not always set, so I want to know what my options are for setting individual, optional view parameters without re-arranging the structure of the function.

In Zend, the following is possible:

$this->view->optionalParameter = $optionalParameter;

Which can go anywhere in the controller method, not just at the end where the view is instantiated. Of course, back in Laravel, I could do something this:

public function show($orderId)
{
    $paramaterArray = [$a, $b, $c];

    if ($sometimesTrue == true)
    {
        $optionalParameter = 'optional parameter';
        $paramaterArray[] = $optionalParameter;
    }

    return view('show', compact($paramaterArray));
}

But re-arranging entire functions because a optional parameter is introduced seems a bit limiting. Is there any way I can set an individual parameter for a view?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire