Laravel: to make external modules independent
We developed a modular project in Laravel 5.1. There are lots of core modules and models that use these modules. In our case, if the inserted module uses other modules, it will be related to those models dynamically.
When I remove the module from project by hand, I need to remove its dependencies from each module. We want establish relations without creating dependency between modules.
For example;
User model from account module used by other many other modules. Assume we have discussion module.
When we build a relationship for discussion model, we can reach the user of the corresponding model. However, if we establish a relationship with the user model, this project will no longer be a moduler.
We want to add dynamic functions to the user module from discussion module.
Temporarily, we add this code fragment to user module.
/**
* @return mixed
*/
public function lastAnswer() {
if( class_exists( Config::get( 'account.models.discussion' ) ) ) {
return $this->hasOne( Config::get( 'account.models.answer' ) )->latest();
}
return null;
}
If the config file has a relation, we link it, otherwise it will not be linked or we will return null.
But we want to add this dinamically from discussion module rather than account module.
If we accomplish this, whenever we add or remove the discussion module from project, it will continue to run without problem.
We tried to add laravel macroable as a trait but we could’t make it work in model files. It gives scope error.
For transformers files we are able to do this but in model files it didn’t work.
from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-to-make-external-modules-independent/
via Lzo Media
No comments:
Post a Comment