Laravel pintruminate class中的覆盖方法

发布于 2025-02-13 18:01:09 字数 449 浏览 0 评论 0原文

我想在Laravel的Illuminate \ Foundation \ vite类中覆盖以下方法:

    /**
     * Generate a script tag for the given URL.
     *
     * @param  string  $url
     * @return string
     */
    protected function makeScriptTag($url)
    {
        return sprintf('<script type="module" src="%s"></script>', $url);
    }

...通过在脚本标签中添加“ defer”属性。我该怎么做,因为这是一个受保护的功能?

I'd like to override the following method in Laravel's Illuminate\Foundation\Vite class:

    /**
     * Generate a script tag for the given URL.
     *
     * @param  string  $url
     * @return string
     */
    protected function makeScriptTag($url)
    {
        return sprintf('<script type="module" src="%s"></script>', $url);
    }

...by adding a "defer" attribute to the script tag. How would I go about doing this, as this is a protected function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2025-02-20 18:01:09

可能是这样的:

<?php
namespace myApp;
use Illuminate\Foundation\Vite as IllVite;

class myClass extends IllVite{
//...
protected function makeScriptTag($url){
    return sprintf('<script type="module" src="%s" defer></script>', $url);
}

//...
}

在调用“ vite”的控制器中,更改

use Illuminate\Foundation\Vite;

use myApp\myClass;

May be like that :

<?php
namespace myApp;
use Illuminate\Foundation\Vite as IllVite;

class myClass extends IllVite{
//...
protected function makeScriptTag($url){
    return sprintf('<script type="module" src="%s" defer></script>', $url);
}

//...
}

In the controller(s) which call "Vite", change :

use Illuminate\Foundation\Vite;

by

use myApp\myClass;
長街聽風 2025-02-20 18:01:09

如果您在应用程序中的另一个位置使用该方法,则SVGTA是正确的。

但是,如果您只想更改该特定的行为,而不是在其他地方使用它,则可以重写原始类,并通过绑定将类替换为app/providers/appserviceprovider

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            'Illuminate\Foundation\Vite', // original class that will be replaced
            'App\VendorRewrites\ViteChanged' // custom class that will be injected
        );
    }
............

另一个可以帮助的帖子:< href =“ https://stackoverflow.com/questions/60841930/laravel-6-7-7-how-can-can-i--i------------- change-a-a- vendor-class”> laravel 6-7我如何覆盖/更改供应商班级?

The svgta is right, if you use that method in another place in your app.

But, if you want to change only that particular behavior, and not to use it in other places, then you can rewrite the original class and replace it by binding changed class to the app in app/Providers/AppServiceProvider:

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            'Illuminate\Foundation\Vite', // original class that will be replaced
            'App\VendorRewrites\ViteChanged' // custom class that will be injected
        );
    }
............

Another post that can help: Laravel 6-7 How Can I Override/Change a Vendor Class?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文