简体   繁体   English

如何使用 Laravel 别名调用非静态方法

[英]How to call non-static method using laravel alias

I have a custom helper class and ill set the alias for that class for access to the blade file(frontend).我有一个自定义的帮助程序类,并没有为该类设置别名以访问刀片文件(前端)。 I can access the static methods referring to the alias::method but I need to access the nonstatic methods from the view.我可以访问引用 alias::method 的静态方法,但我需要从视图访问非静态方法。 how can I do that?我怎样才能做到这一点?

'Access' => App\Services\Access::class,
Two functions in Access class.
public function getPermissions()
public static function getUser()

I can easily access the static function using Access::getUser() So How can access the non-static functions?我可以使用Access::getUser()轻松访问静态函数 那么如何访问非静态函数呢?

If you just want to access it in your view, you can share a new instance of Access to all your views :如果您只想在您的视图中访问它,您可以共享一个新的 Access 实例来访问您的所有视图:

In the boot() method of your AppServiceProvider :在 AppServiceProvider 的 boot() 方法中:

View::share('access', (new App\Services\Access));

then in your blade @foreach($access->getPermissions() as $permission)然后在你的刀片中@foreach($access->getPermissions() as $permission)

You can inject an instance of this class into your view:您可以将此类的实例注入您的视图:

@inject('access', Access::class)

Now you can access all those methods via $access->whatEverMethod() .现在您可以通过$access->whatEverMethod()访问所有这些方法。 Otherwise you will need a Facade or deal with the magic methods to call the non static method on the class itself.否则,您将需要 Facade 或处理魔术方法来调用类本身的非静态方法。

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

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