Blade if condition in controller function

2019-09-07 06:57发布

问题:

Is it possible to use blade @if conditions in functions of controller. This is my function of PublicController

    public function tourBanner($country){

    @if( $country == 'country1')
        background-image:url({{ asset('images/inside8.jpg') }});                        
    @elseif ( $country == 'country2')           
            background-image:url({{ asset('images/inside7.jpg') }});

    @elseif ( $country == 'country3')           
            background-image:url({{ asset('images/inside5.jpg') }});

    @elseif ( $country == 'country4')           
            background-image:url({{ asset('images/inside6.jpg') }});            
    @else           
            background-image:url({{ asset('images/inside10.jpg') }});           
    @endif

    return banner;
}

And i'm getting this error

Parse error: syntax error, unexpected 'if' (T_IF)

What mistake i have made or we can't use blade if if statements in controller ?

回答1:

The syntax of Blade @if is made for using in blade views not into the php files (as in your case controllers). You should use Blade Syntax in to files ending with extension - filename.blade.php, and in you controller please use normal PHP syntax for if statement.

Please see Blade Docs

class Controller {
   function a() {
      if($country == 'country1') {
          $asset_str = asset('images/inside8.jpg');
          $banner = "background-image:url({$asset_str})";
      }
      ....
      return $banner;
   }
}

Hope this helps!



回答2:

Typos within your code that code is meant for blade files and not for your PHP files. Listing issues within your code

Remove @ from your code

@if .... @elsif

should be

if .... elseif 

better for multiple if ... else you can use switch case

You were returning banner but it should be $banner and also you were not assigning any value to $banner variable. So your code be like as

if( $country == 'country1')
       $banner =  "background-image:url(".asset('images/inside8.jpg') .")";
.......

return $banner;

better to use switch case like as

switch($country)
{
     case "country1":
          $banner =  "background-image:url(".asset('images/inside8.jpg') .")";
          break;
     case ..............
}


回答3:

In controller you should write like this:

if ($country == 'country1') {
    $a = 'background-image:url('.asset('images/inside8.jpg').')';
} elseif ....

But if you're gonna to build HTML/CSS/JS with PHP, it's terrible idea, do not do that.



回答4:

You could build up the style within your controller and then pass the resulting data to your view from that Controller and then render it. This snippet may give you a little insight:

<?php

    public function tourBanner($country){
        $extraData  = ['zero', 'one', 'two'];

        switch($country){
            case 'country1':
                $bgImage    = 'images/inside8.jpg';
                break;
            case 'country2':
                $bgImage    = 'images/inside7.jpg';
                break;
            case 'country3':
                $bgImage    = 'images/inside5.jpg';
                break;
            case 'country4':
                $bgImage    = 'images/inside6.jpg';
                break;
            default:
                $bgImage    = 'images/inside10.jpg';
                break;
        }

        return view("your.blade.view", array(
            "bgImage"    => $bgImage,
            'extraData'  => $extraData,
        ));
    }

Then; in the View, it would be a matter of doing something like this:

    <div class="container" style="background-image:url({{ asset($bgImage) }})">
        <div class="col-md-12">
            Some Content...
        </div>
    </div>


回答5:

Please try switch case for above condition.

switch ($country) {
    case "country1":
        echo "background-image:url({{ asset('images/inside8.jpg') }})";
        break;
}

It will return exactly the same results.



回答6:

NO, you cant. just use the default if else condition in php.



回答7:

Blade is a template parser, which converts code to PHP behind the scenes. If you want to use some if statement in your controller you just use the standard IF statement.

if($q == $a) {
    #more code here
} else {
    $other code here
} 

As suggested above, it's recommended using a switch statement when using a lot of else statements for your if

switch($q){
    case "yes": 
        return $something;
    break;
    case "no":
        return $somethingelse;
        break;
    case "maybe":
        return $whoknows;
        break;
}