Traits
For quick controller generation, use any of the following traits:
Api
This trait makes available all resource methods necessary for api endpoints.
Abstract Methods
Protected methods
model()
andvalidationRules(array $data, $id = null)
MUST be implemented.
<?php
namespace App\Http\Controllers;
use Laraquick\Controllers\Traits\Api;
use App\Task;
class TaskController extends Controller {
use Api;
protected function model() {
return Task::class;
}
protected function validationRules(array $data, $id = null) {
return [
'title' => 'required|string|max:50',
'content' => 'sometimes|string',
'deadline' => 'sometimes|date'
];
}
}
With only the two (required) methods above, the controller is ready to handle api requests.
PassThrough
This trait allows passing all REST endpoints to another server.
Abstract Methods
Protected methods
headers()
andtoUrl()
MUST be implemented.
<?php
namespace App\Http\Controllers;
use Laraquick\Controllers\Traits\PassThrough;
use App\Task;
class TaskController extends Controller {
use PassThrough;
protected function headers() {
return [
'Authorization' => 'Bearer: some-token-here'
];
}
protected function toUrl() {
return 'http://example.com/api';
}
}
All BREAD operations that reach the controller are forwarded to provided url with the given headers.
Web
This trait makes available important resource methods necessary for the web. Methods not handled include create
and edit
.
Abstract Methods
Protected methods
model()
andvalidationRules(array $data, $id = null)
MUST be implemented.
<?php
namespace App\Http\Controllers;
use Laraquick\Controllers\Traits\Web;
use App\Task;
class TaskController extends Controller {
use Web;
protected function model() {
return Task::class;
}
protected function validationRules(array $data, $id = null) {
return [
'title' => 'required|string|max:50',
'content' => 'sometimes|string',
'deadline' => 'sometimes|date'
];
}
public function create() {
// implement
}
public function edit($id) {
// implement
}
}
And with only these two (required) methods above, the controller is ready.
Updated over 6 years ago