laravel 自定义类添加别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32435307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:18:54  来源:igfitidea点击:

laravel custom classes add alias

phpclasslaravelalias

提问by VGD

So I want to add a alias for custom classes in order to be able to use them in blade:

所以我想为自定义类添加一个别名,以便能够在刀片中使用它们:

<?php
namespace App\Classes;

class Requirement
{
    public static function Test()
    {
        return "hello";
    }
} 

In config/app.phpI added a alias like so:

config/app.php我添加了一个别名,如下所示:

...
'Requirement' => App\Classes\Requirement::class

Then, I would like to be able to call it in a blade template like

然后,我希望能够在刀片模板中调用它,例如

{{ Requirement::Test() }}

But the alias is not working somehow. I also tried composer dump-autoload, but it's still not working.

但是别名以某种方式不起作用。我也试过了composer dump-autoload,但还是不行。

BTW: Is adding custom classes like that a viable way to implement site-specific logic like retrieving and processing data from a database or is there a better approach?

顺便说一句:添加这样的自定义类是实现特定于站点的逻辑(例如从数据库检索和处理数据)的可行方法,还是有更好的方法?

Edit 1

编辑 1

I created Requirement.phpin app/Facadeswith following content

我创建Requirement.phpapp/Facades与以下内容

<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;

class Requirement extends Facade{
    protected static function getFacadeAccessor() { return 'Requirement'; }
}

added PageContentProvider.phpin app/Providerswith the following content

PageContentProvider.phpapp/Providers包含以下内容

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class PageContentProvider extends ServiceProvider
{
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('Requirement', function($app){
            return new \App\Classes\Requirement();
        });
    }
}

and in config/app.phpthe alias

并在config/app.php别名中

'Requirement'=>App\Facades\Requirement::class

as well as the provider

以及提供者

App\Providers\PageContentProvider::class

but it's still not working.

但它仍然无法正常工作。

Edit 2

编辑 2

By adding something like

通过添加类似的东西

exit();

or

或者

echo "blabla";

inside register(), nothing changes. Does that indicate that PageContentProvideris not even getting loaded?

里面register(),没有任何变化。这是否表明它PageContentProvider甚至没有被加载?

Edit 3

编辑 3

Since the standard AppServiceProvidergets loaded, I deleted the coresponding entry of AppServiceProviderin config/app.php... and it still worked!Somehow my changes don't get applied. Does anybody have solution for that?

由于标准AppServiceProvider被加载,我删除了AppServiceProviderin config/app.php...的相应条目,它仍然有效!不知何故,我的更改没有得到应用。有人有解决方案吗?

回答by VGD

Problem wasn't the code at all: After realising that changes in config/app.phpdidn't get applied, a simple

问题根本不是代码:在意识到config/app.php没有应用更改后,一个简单的

php artisan config:clear

fixed literally every issue I presented in my question.

从字面上解决了我在问题中提出的每个问题。

回答by Iamzozo

Add to your composer file:

添加到您的作曲家文件:

"autoload": {
    //...
    "files" : ["app/classes/Requirement.php"]
},

Then add to your alias as you wrote in your config/app.php

然后添加到您在 config/app.php 中写的别名

Then you will be able to use in your templates:

然后您将能够在您的模板中使用:

{{ Requirement::test() }}

回答by Douglas.Sesar

Since you mentioned that you needed a class in Blade:

既然你提到你需要在 Blade 中开设一个课程:

In stead of setting up an alias, you could use the blade @injectfunction at the top of your blade file.

您可以使用@inject刀片文件顶部的刀片功能,而不是设置别名。

@inject('your_class','App\Helpers\SomeNamespace\Yourclass')

Then in the blade template:

然后在刀片模板中:

{{$your_class->doSomething()}}

回答by aldrin27

Try this if this works:

如果可行,请尝试此操作:

In your controller:

在您的控制器中:

 $whateveryour_variable = Requirement::Test();
 return view('yourview',compact('whateveryour_variable'));

In your View:

在您的视图中:

   {{$whateveryour_variable}}

You could fetch the data in your db then store it in a variable then pass that in your view.

您可以在您的数据库中获取数据,然后将其存储在一个变量中,然后将其传递到您的视图中。

回答by Borja Marco

Did you tried with a slash?

你用斜线试过吗?

{{ \Requirement::Test() }}

EDIT: grammar

编辑:语法

回答by Martin Bean

What you're looking for is Fa?ades.

您要找的是Fa?ades

You create a Fa?ade class that contains a reference to your class's binding in the service container, and that's what you use in the aliasesarray in your config/app.phpfile.

您创建了一个 Fa?ade 类,其中包含对服务容器中类绑定的引用,这就是您aliasesconfig/app.php文件中的数组中使用的内容。

Documentation: http://laravel.com/docs/5.1/facades

文档:http: //laravel.com/docs/5.1/facades