【问题标题】:Extend Laravel package扩展 Laravel 包
【发布时间】:2018-06-08 07:11:36
【问题描述】:

我四处寻找,找不到明确的答案...

我有一个包 DevDojo Chatter,并希望使用我的应用程序对其进行扩展。我知道我必须覆盖这些函数,以便作曲家更新不会覆盖我的更改。

我该怎么做?

更新

public function store(Request $request)
{
    $request->request->add(['body_content' => strip_tags($request->body)]);

    $validator = Validator::make($request->all(), [
        'title'               => 'required|min:5|max:255',
        'body_content'        => 'required|min:10',
        'chatter_category_id' => 'required',
    ]);

    Event::fire(new ChatterBeforeNewDiscussion($request, $validator));
    if (function_exists('chatter_before_new_discussion')) {
        chatter_before_new_discussion($request, $validator);
    }

    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    }

    $user_id = Auth::user()->id;

    if (config('chatter.security.limit_time_between_posts')) {
        if ($this->notEnoughTimeBetweenDiscussion()) {
            $minute_copy = (config('chatter.security.time_between_posts') == 1) ? ' minute' : ' minutes';
            $chatter_alert = [
                'chatter_alert_type' => 'danger',
                'chatter_alert'      => 'In order to prevent spam, please allow at least '.config('chatter.security.time_between_posts').$minute_copy.' in between submitting content.',
                ];

            return redirect('/'.config('chatter.routes.home'))->with($chatter_alert)->withInput();
        }
    }

    // *** Let's gaurantee that we always have a generic slug *** //
    $slug = str_slug($request->title, '-');

    $discussion_exists = Models::discussion()->where('slug', '=', $slug)->first();
    $incrementer = 1;
    $new_slug = $slug;
    while (isset($discussion_exists->id)) {
        $new_slug = $slug.'-'.$incrementer;
        $discussion_exists = Models::discussion()->where('slug', '=', $new_slug)->first();
        $incrementer += 1;
    }

    if ($slug != $new_slug) {
        $slug = $new_slug;
    }

    $new_discussion = [
        'title'               => $request->title,
        'chatter_category_id' => $request->chatter_category_id,
        'user_id'             => $user_id,
        'slug'                => $slug,
        'color'               => $request->color,
        ];

    $category = Models::category()->find($request->chatter_category_id);
    if (!isset($category->slug)) {
        $category = Models::category()->first();
    }

    $discussion = Models::discussion()->create($new_discussion);

    $new_post = [
        'chatter_discussion_id' => $discussion->id,
        'user_id'               => $user_id,
        'body'                  => $request->body,
        ];

    if (config('chatter.editor') == 'simplemde'):
       $new_post['markdown'] = 1;
    endif;

    // add the user to automatically be notified when new posts are submitted
    $discussion->users()->attach($user_id);

    $post = Models::post()->create($new_post);


    if ($post->id) {
        Event::fire(new ChatterAfterNewDiscussion($request));
        if (function_exists('chatter_after_new_discussion')) {
            chatter_after_new_discussion($request);
        }

        if($discussion->status === 1) {
            $chatter_alert = [
                'chatter_alert_type' => 'success',
                'chatter_alert'      => 'Successfully created a new '.config('chatter.titles.discussion').'.',
            ];
            return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
        } else {
            $chatter_alert = [
                'chatter_alert_type' => 'info',
                'chatter_alert'      => 'You post has been submitted for approval.',
            ];
            return redirect()->back()->with($chatter_alert);
        }

    } else {
        $chatter_alert = [
            'chatter_alert_type' => 'danger',
            'chatter_alert'      => 'Whoops :( There seems to be a problem creating your '.config('chatter.titles.discussion').'.',
            ];

        return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
    }
}

供应商包中有一个我想修改/覆盖的存储功能。如果需要,我希望能够修改某些功能或部分功能。请有人指出我正确的方向。

【问题讨论】:

    标签: laravel laravel-5 composer-php package extend


    【解决方案1】:

    如果您的意思是修改应用程序中的类实现,您可以更改解析类的方式:

    app()->bind(PackageClass:class, YourCustomClass::class);
    

    现在您可以像这样创建这个自定义类:

    class YourCustomClass extends PackageClass
    {
       public function packageClassYouWantToChange()
       {
           // here you can modify behavior
       }
    }
    

    我建议您阅读有关binding 的更多信息。

    当然很大程度上取决于类是如何创建的,如果它是使用new 运算符创建的,您可能需要更改多个类,但如果它是注入的,那么更改单个类就足够了。

    【讨论】:

    • 这真的没有多大帮助。我试过了,但没有用。
    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 2018-04-02
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    相关资源
    最近更新 更多