A simple Laravel trait with restful crud implementation.
$ composer require moraesgil/api-restful-trait
{
"require": {
"moraesgil/api-restful-trait": "~1.0"
}
}
create your routes in routes/api.php or your prefer routes file
Route::resource('/samples', 'YourLaravelController', ['except' => ['create', 'edit', 'show']]);
make sure you have a model at App\Entities\YourModel same name of controller to Autoload model entity, or you can inject dependencies, see bellow the following instructions in Extras area...
//terminal
php artisan make model YourLaravel
In your Controller
use Traits\Controllers\ApiRestfulTrait; //<<<<< add this line
class YourLaravelController extends Controller {
use ApiRestfulTrait; //<<<<< add this line
// ..others non-crud methods
}
Include the EntityValidatorTrait it's a dependency. Please check documentation for more info. Also read about Autoload model entity in this extras section.
use Traits\Entities\EntityValidatorTrait; //<<<<< add this line
class YourLaravel extends Model {
use EntityValidatorTrait; //<<<<< add this line
}
May you have to call a page in index() if not ajax request, we have getPage(), can you override method or just to pass data by constructor, also you can redirect to another page passing the route name;
//in or controller you can pass
class YourLaravelController extends Controller {
public function __construct()
{
//Passing view and data
$this->indexView = "youCrudBlade"; //default "crud"
$this->indexData = [
"pageTitle" => "Page title for exemple"
];
// OR redirect to named route
$this->indexRedirectRouteName ="myroute.indexpage"
}
}
By default it try access a model with same name of controller, you can to pass your entity by name or object in constructor
class YourLaravelController extends Controller {
public function __construct(Mymodel $m)
{
$this->Model = $m;
// or
$this->modelName = "Mymodel";
// or Just create a model same name of controller, from this controller sample "YourLaravel"
}
}
By default it return a Laravel paginate getting limit by request, can you override method or call a customMethod from your entitiy passing $modelGetMethod
class YourLaravelController extends Controller {
public function __construct()
{
$this->modelGetMethodName = "myCustomPaginate";
}
}
- EntityValidatorTrait - My trait to allow model do self validation - EntityValidatorTrait
- Gilberto Prudêncio Vaz de Moraes - Initial work - MoraesGil
This project is licensed under the MIT License - see the LICENSE file for details