Extend Laravel package

laravel laravel-5 composer-php package extend

64 观看

1回复

54 作者的声誉

I've searched around and couldn't find a definitive answer for this...

I have a package DevDojo Chatter and would like to extend it using my application. I understand I'd have to override the functions so that a composer update doesn't overwrite my changes.

How do I go about doing this?

UPDATE

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);
    }
}

There's a store function within the vendor package that i'd like to modify/override. I want to be able to modify some of the function or perhaps part of it if needed. Please someone point me in the right direction.

作者: sogeniusio 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

79078 作者的声誉

If you mean modify class implementation in your application you can change the way class is resolved:

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

and now you can create this custom class like so:

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

I would advise you to read more about binding.

Of course a lot depends on how class is created, if it is created using new operator you might need to change multiple classes but if it's injected it should be enough to change this single class.

作者: Marcin Nabiałek 发布者: 2017 年 12 月 27 日
32x32