Package Data | |
---|---|
Maintainer Username: | pionl |
Maintainer Contact: | martin.kluska@imakers.cz (Martin Kluska) |
Package Create Date: | 2016-02-02 |
Package Last Update: | 2016-06-21 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 15:04:33 |
Package Statistics | |
---|---|
Total Downloads: | 32 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
A few lines of code to create a form/create page via the repository way. In default the controller supports all the methods for a CRUD (missing the index/show page, you can implement it on your end). The controller uses the repository class that holds all the standart methods for the model, just create a Repository class for your eloquent model, return the class and thats all.
Laravel Framework (5.1, 5.2) - 5.0 not tested
composer require pion/laravel-repository-controllers
Add the service provider to copy the translations to the app.php config
Pion\Repository\RepositoryServiceProvider::class
Run the publish command to copy the translations (Laravel 5.2)
php artisan publish --provider="Pion\Repository\RepositoryServiceProvider"
Run the publish command to copy the translations (Laravel 5.1)
php artisan vendor:publish --provider="Pion\Repository\RepositoryServiceProvider"
In construct of the controller use the bootRepository() to prepare the repository data. This will also use translated
titles and etc. The best practive is to build your own RepositoryController that uses the trait. Then you can extend the
methods fromt trait like a normal class. For default usage you can extend provided RepositoryController
/**
* RepositoryController constructor.
*/
public function __construct()
{
parent::__construct();
// required!
$this->bootRepository();
$this->formView = "admin.season.form"; // the desired form
// optional
$this->editTitle = "Editing this entry"; // call after boot!
$this->formObjectIndex = "season"; // the index of the object in form
$this->detailModelAction = "edit"; // instead of showing the detail action to show, use the edit page
$this->setRedirectToEditOnCreate(true);
}
or you can use
/**
* RepositoryController constructor.
*/
public function __construct()
{
parent::__construct();
$this->bootRepositoryWith("admin.season.form", "Create new!", "Edit this one");
}
Implement the createRepository method. You can/need to return the repository
/**
* Creates a AreaRepository repository
* @return AreaRepository
*/
protected function createRepository()
{
return new AreaRepository(); // this will use the RepositoryTrait
}
You can overide the form data for edit/create and both usages by extending these functions.
/**
* Prepares the form data for all the views
* @param array $data
* @param Model|null $object
* @param Request $request
*/
protected function prepareFormData(array &$data, $object, $request) {}
/**
* Prepares the data only for the
* @param array $data
* @param Model $object
* @param Request $request
*/
protected function prepareEditFormData(array &$data, $object, $request) {}
/**
* Prepares the data for create form
* @param array $data
* @param Request $request
*/
protected function prepareCreateFormData(array &$data, $request) {}
Supported since 0.9.3 version
You can customize the own navigation via methods (you can or not call the parent method with the trait subclass):
/**
* Generates the navigation for create action
* @param Request $request
*/
protected function createNavigationForCreateAction(Request $request)
{
$this->createNavigation($this->createTitle);
}
/**
* Generates the navigaiton for edit action
* @param Request $request
* @param Model $object
*/
protected function createNavigationForEditAction(Request $request, $object)
{
$this->createNavigation($this->editTitle, $object);
}
Very similar boot usage as repository controller with more options:
/**
* Defines the index for the parent object in the form view. Must be in the
* data of the model (witout the _id)
* @var string
*/
protected $formParentObjectIndex = "parent";
/**
* Defines a list of collumns we want to select from the parent. If empty
* the repository will use the name collumn and the key
* @var array
*/
protected $parentObjectSelect = [];
/**
* Defines the collumn for the parent object name
* @var string
*/
protected $parentObjectNameCollumn = "name";
You can overide the form data for edit/create and both usages by extending these functions.
/**
* Prepares the form data for all the views
* @param array $data
* @param Model|null $object
* @param Model $parentObject
*/
protected function prepareFormData(array &$data, $object, $parentObject) {}
/**
* Prepares the data only for the
* @param array $data
* @param Model $object
* @param Request $request
* @param Model $parentObject
*/
protected function prepareEditFormData(array &$data, $object, $request, $parentObject) {}
/**
* Prepares the data for create form
* @param array $data
* @param Request $request
* @param Model $parentObject
*/
protected function prepareCreateFormData(array &$data, $request, $parentObject) {}
The repository instance that will handle create/update/destroy and all the form validation needed. You can create own BaseRepository that uses the RepositoryTrait and all the repositories will extend the BaseRepository.
For checkbox validation you can add a list of checkbox indexes that will be validated and will prefill 0 or 1 for the unchecked/checked state. Unchecked the checkboxes will be added to the request with 0 value.
You must return the model class
/**
* @return string
*/
protected function getClass()
{
return Area::class;
}
A set of validation rules. You can return empty array.
protected function getValidationRules($isNew, array $data, $model = null)
{
return [
"name" => "required|string|max:255",
"position" => "required|numeric"
];
}
You must use the desired trait and change the traits method you want to extend. Then you can call the method with your own method.
use ChildRepositoryControllerTrait {
store as baseStore;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $parentId
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $parentId)
{
// redirect to new form with old data so we can create same properties easier
return $this->baseStore($request, $parentId)->withInput($request->except([
"name", "position", "identifier"
]));
}