Is this controller with ACF perfectly acceptable

Hi,

I have read lots of documentation now but I’m still not sure if I’m doing the right thing. I have written a basic controller. My concern is that the user won’t fill in the ACF fields and the theme will error out. I want a conditional or return without error.
The ACF field is a group.

Cheers for any assistance.

Controller

public function hero()
{
  $hero = get_field('hero'); // ACF Group
  return (object) [
    'background' => $hero['background']['url'],
    'headline' => $hero['headline'] ?: App::title(),
    'lead' => $hero['lead'],
    'button' => (object) [
      'url' => $hero['button']['url'],
      'title' => $hero['button']['title'] ?: 'Book an Appointment',
      'target' => $hero['button']['target'] ?: '_self',
    ]
  ];
}

Blade Template

@if($hero)
<section id="welcome" class="e-hero" style="background-image: linear-gradient(180deg, rgba(0, 0, 0, 0.1) 0%, rgba(0, 0, 0, 0.2) 100%), url('{{ $hero->background }}');">
  <div class="container-fluid h-100">
    <div class="row justify-content-center align-items-center h-100">
      <div class="col-11 col-sm-10 col-md-9 col-lg-8 text-center text-white">
        <p class="display-4 tagline mb-5 animated fadeInUp delay-1s">{!! $hero->headline !!}</p>
        <p class="lead mb-5 animated fadeInUp delay-2s">{!! $hero->lead !!}</p>
        @if($hero->button)
          <a href="{{ $hero->button->url }}" class="btn btn-lg btn-orange shadow-lg animated fadeInUp delay-3s" rel="button" target="{{ $hero->button->target }}">{{ $hero->button->title }}</a>
        @endif
      </div>
    </div>
  </div>
</section>
@endif

Have you tested it to see what will happen? That’s generally the best way.

Yes, it works. I guess this is all acceptable. :slight_smile:

This topic was automatically closed after 42 days. New replies are no longer allowed.