如何通过“使用"使用自定义类.在Laravel的5个刀片模板中?
我在Windows和基于Linux的服务器上都有一个Web服务器.当我在Windows上启动Laravel 5项目时,一切正常,但是我在Linux(ubuntu)服务器上遇到了麻烦,在托管上也遇到了同样的麻烦.当我尝试加载索引页面时,出现如下错误:
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:
未找到"App \ Helpers \ Substr"类
Class 'App\Helpers\Substr' not found
发生这种情况是因为我在刀片模板中使用了自定义帮助程序,并且已经通过"use"运算符加载了它,如下所示:
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?
首先,请确保您的类是通过Composer自动加载的.
First, make sure your classes are autoloaded via Composer or so.
然后,您可以将命名空间类添加到config/app.php
中的'aliases'
数组中,如下所示:
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.