简体   繁体   English

如何在Laravel的5个刀片模板中通过“use”使用自定义类?

[英]How to use custom classes via “use” in Laravel's 5 blade templates?

I have a web-server on windows and Linux based server. 我在Windows和基于Linux的服务器上有一个Web服务器。 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. 当我在Windows上启动Laravel 5项目时,一切正常,但我在Linux(ubuntu)服务器上遇到了麻烦,我的托管也遇到了同样的麻烦。 When I'm trying to load the index page I'm getting an error like this: 当我尝试加载索引页面时,我收到如下错误:

Class 'App\\Helpers\\Substr' not found 找不到“App \\ Helpers \\ Substr”类

It happened because I'm using custom helpers in my blade templates and had been loading it via the "use" operator like this: 之所以发生这种情况,是因为我在我的刀片模板中使用了自定义帮助程序,并通过“use”运算符加载它,如下所示:

<?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? 那么,在项目完成后,在这种情况下可能是一个很好的解决方案?

First, make sure your classes are autoloaded via Composer or so. 首先,确保您的类通过Composer左右自动加载。

Then, you can add your namespaced classes to the 'aliases' array in config/app.php , like this: 然后,您可以将命名空间类添加到config/app.php'aliases'数组中,如下所示:

'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. 您可以根据需要为别名命名。

I'm guessing the problem is that your file system on your Windows machine isn't case sensitive. 我猜测问题是你的Windows机器上的文件系统不区分大小写。 Therefore, when you try to load a class like App\\Helpers\\Substr , while you actually meant App\\Helpers\\SubStr , or the file path does not correspond exactly with the class name when it comes to case (PSR-4), your class will still load on Windows. 因此,当您尝试加载类似App\\Helpers\\Substr App\\Helpers\\SubStr的类时,实际上意味着App\\Helpers\\SubStr ,或者文件路径与案例名称(PSR-4)时的类名不完全对应,类仍会在Windows上加载。

Linux however is less forgiving, and has a filesystem that is case sensitive (at least my dev Ubuntu version does, but I guess it is the default for Linux). 然而Linux不太宽容,并且有一个区分大小写的文件系统(至少我的开发Ubuntu版本,但我想它是Linux的默认值)。

So what you should do to fix this is track down your "case typo(s)" and fix it. 那么你应该做些什么来解决这个问题就是追查你的“案例拼写错误”并修复它。

To prevent these issues, and catch them as soon as you make the mistake, I would strongly suggest to only develop on a case sensitive file system. 为了防止这些问题,并在你犯错误时立即捕获它们,我强烈建议只在一个区分大小写的文件系统上进行开发。 I know you can make a case sensitive partition on mac, so I would guess something similar is possible somehow for Windows. 我知道你可以在mac上创建一个区分大小写的分区,所以我猜想类似的东西可能会以某种方式用于Windows。 Or you could work in a Linux VM like homestead while developing. 或者你可以在开发时像家园一样在Linux VM中工作。 I'm a big fan of keeping your dev environment as close to the production environment as possible. 我非常喜欢让您的开发环境尽可能贴近生产环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM