lundi 23 mai 2016

Use custom class in Laravel 5

In app/Library I have a file called Helpers.php

<?php

namespace App\Library\Helpers;

class Helpers {
  public function truncate($text) {
    if (strlen($text) > 200) {
      preg_replace('/\s+?(\S+)?$/', '', substr($text, 0, 201));
    }
    return $text;
  }
}

In one of my views, I want to use the class:

  @foreach($data['articles'] as $article)
    <div class="container">
      <div class="article">
        <h1></h1>
        <p>
          <?php
            $helpers = new \App\Library\Helpers;
            echo $helpers->truncate($article->body);
          ?>
        </p>
      </div>
    </div>
  @endforeach

I get this error:

Class 'App\Library\Helpers' not found



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1s4vOGh
via IFTTT

Aucun commentaire:

Enregistrer un commentaire