Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom classes via "use" in Laravel's 5 blade templates?

I have a web-server on windows and Linux based server. When I'm launching Laravel 5 project on windows everything works fine, but I have a trouble with Linux (ubuntu) server and the same trouble on my hosting. When I'm trying to load the index page I'm getting an error like this:

Class 'App\Helpers\Substr' not found

It happened because I'm using custom helpers in my blade templates and had been loading it via the "use" operator like this:

<?php

use App\Helpers\Substr;
use App\Helpers\LoaderBtn;

?>

@extends('zaks.public')

@section('content')

@include('zaks.search')

So, what might be a good solution in this situation when the project has been finished?

like image 221
Nesquik27 Avatar asked Dec 10 '22 15:12

Nesquik27


1 Answers

First, make sure your classes are autoloaded via Composer or so.

Then, you can add your namespaced classes to the 'aliases' array in config/app.php, like this:

'aliases' => array(
   // other aliases...
   'App_Helper_Substr' => 'App\Helpers\Substr',
);

and then use it right in your view the regular way:

App_Helper_Substr->something...
App_Helper_Substr::something();

You can name your aliases whatever you want.

like image 146
Paul Denisevich Avatar answered Jan 29 '23 16:01

Paul Denisevich