如何通过“使用"使用自定义类.在Laravel的5个刀片模板中? [英] How to use custom classes via "use" in Laravel's 5 blade templates?

查看:70
本文介绍了如何通过“使用"使用自定义类.在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.

这篇关于如何通过“使用"使用自定义类.在Laravel的5个刀片模板中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆