How to override a model class in PyroCMS (Laravel, PHP)?

青春壹個敷衍的年華 提交于 2019-12-04 02:15:29

问题


I installed PyroCMS and am extending it to make it into a Learning Management System (LMS) where only logged-in users can view the pages, and the pages also only begin to be viewable a variable number of days after a user enrolls in the course.

(I.e., Module 1's Lesson 1 may unlock and be visible immediately, but Lesson 2 could be configured to be hidden until 1 day later, and Lesson 3 might become visible X days later, etc.)

How I achieved this was by writing a Laravel package with this migration:

Schema::table('pages_pages', function (Blueprint $table) {
    $table->string('drip_delay')->nullable()->after('str_id');
});

I then created a DrippablePagesServiceProvider class with this in the boot() function:

$this->app->bind('Anomaly\PagesModule\Http\Controller\PagesController', 'me\DrippablePages\PagesController'); //https://laravel.com/docs/5.6/container#binding     

I designed my custom PagesController to show a special view whenever the logged-in user is trying to access a page too early. This functionality is all working totally fine.

But instead of editing the drip_delay field directly in the database like I've been doing, I'd prefer to be able to edit right alongside the other fields at the /admin/pages/edit/4 URL.

I'm pretty sure I need to override various parts of PagesModule, such as PageEntryFormSections (doc). And I think I have that working.

But when stepping through with Xdebug, I see that the PageModel that gets looked up at this line (via dependency injection?†) in edit() within Http\Controller\Admin\PagesController still doesn't show my new drip_delay field.

How can I override PageModel or do whatever I need to do so that it shows the drip_delay field in this Admin panel view?

† Laravel docs about container and controllers imply this.


回答1:


To override a model first you need a new one which extends a model you want to override:

<?php namespace Ryan\ExtenderModule\Post;

class PostModel extends \Anomaly\PostsModule\Post\PostModel
{

}

Then inside the ServiceProvider you need to bind it reversed:

<?php namespace Ryan\ExtenderModule;

use Anomaly\PostsModule\Post\PostModel;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;

class ExtenderModuleServiceProvider extends AddonServiceProvider
{
    protected $bindings = [
        PostModel::class => \Ryan\ExtenderModule\Post\PostModel::class,
    ];
}

That's all. Good luck ))



来源:https://stackoverflow.com/questions/50518784/how-to-override-a-model-class-in-pyrocms-laravel-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!