Last active
March 1, 2016 18:26
-
-
Save krisanalfa/479c6f67bb8a6092dd3a to your computer and use it in GitHub Desktop.
In replied to Mulia Nasution as response to my work partner Syaifudin Latief.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Http\Controllers\Cart; | |
| use Illuminate\Support\Facades\View; | |
| use App\Http\Requests\Cart\StoreRequest; | |
| use Illuminate\Support\Facades\Redirect; | |
| use App\Http\Requests\Cart\UpdateRequest; | |
| use App\Cart\Persistances\Cart as Persistant; | |
| use App\Cart\Repositories\Cart as Repository; | |
| /** | |
| * In replied to Mulia Nasution <https://www.facebook.com/groups/laravel/permalink/639129009558792> | |
| * as response to my work partner Syaifudin Latief. Ini hanya gambaran kasar aja, | |
| * dan GAK akan langsung works di coding-an kalian, dengan kata lain, ini semacam | |
| * pseudo code. | |
| * | |
| * @author Krisan Alfa Timur <krisan47[at]gm*il[dot]c*m> | |
| * | |
| * @see https://laravel.com/docs/5.2/facades Facades | |
| * @see https://laravel.com/docs/5.2/validation#form-request-validation Form Request Validation | |
| */ | |
| class ResourceController | |
| { | |
| /** | |
| * Fungsi dari object ini untuk fetch data aja dari database. Sifatnya read only. | |
| * | |
| * @var \App\Cart\Repositories\Cart | |
| */ | |
| protected $repository; | |
| /** | |
| * Fungsi dari object ini untuk menulis data ke database. Sifatnya read and write. | |
| * | |
| * @var \App\Cart\Persistances\Cart | |
| */ | |
| protected $persistant; | |
| /** | |
| * Class constructor. Inject semua kebutuhan controller di sini. | |
| * | |
| * @param \App\Cart\Repositories\Cart $repository Fetch data (Read only). | |
| * @param \App\Cart\Persistances\Cart $persistant Persistant data (Read and write). | |
| */ | |
| public function __construct(Repository $repository, Persistant $persistant) | |
| { | |
| $this->repository = $repository; | |
| $this->persistant = $persistant; | |
| } | |
| /** | |
| * Tampilin single resource. Untuk mencari cart yang mau kita tampilin, | |
| * kita bisa menggunakan method 'get' di class 'Repository'. | |
| * | |
| * @param int $id ID cart di database | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function show($id) | |
| { | |
| $cart = $this->repository->get($id); | |
| return View::make('cart.show', compact('cart')); | |
| } | |
| /** | |
| * Tampilin list cart yang ada. Untuk mencari daftar cart yang mau kita | |
| * tampilin, kita bisa menggunakan method 'all' di class 'Repository'. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function search() | |
| { | |
| $carts = $this->repository->all(); | |
| return View::make('cart.list', compact('carts')); | |
| } | |
| /** | |
| * Tambah cart baru. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function add() | |
| { | |
| return View::make('cart.add'); | |
| } | |
| /** | |
| * Simpen cart baru ke database melalui 'Persistant' class. | |
| * | |
| * @param \App\Http\Requests\Cart\StoreRequest $request Untuk validasi input dan allowing request. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function store(StoreRequest $request) | |
| { | |
| $this->persistant->store($request->only([ | |
| 'foo', // nama field | |
| 'bar', // nama field | |
| 'baz', // nama field | |
| ])); | |
| return Redirect::to('/cart/list'); | |
| } | |
| /** | |
| * Tampilin form untuk edit cart. Untuk mencari cart yang mau kita edit, | |
| * kita bisa menggunakan method 'get' di class 'Repository'. | |
| * | |
| * @param int $id ID cart di database. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function edit($id) | |
| { | |
| $cart = $this->repository->get($id); | |
| return View::make('cart.edit', compact('cart')); | |
| } | |
| /** | |
| * Update cart ke dalam database melalui 'Persistant' class. | |
| * | |
| * @param int $id ID cart di database. | |
| * @param \App\Http\Requests\Cart\UpdateRequest $request Untuk validasi input dan allowing request. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function update($id, UpdateRequest $request) | |
| { | |
| $this->persistant->update($id, $request->only([ | |
| 'foo', // nama field | |
| 'bar', // nama field | |
| 'baz', // nama field | |
| ])); | |
| return Redirect::to('/cart/list'); | |
| } | |
| /** | |
| * Delete cart di database melalui 'Persistant' class. | |
| * | |
| * @param int $id ID cart di database. | |
| * | |
| * @return \Illuminate\Http\Response | |
| */ | |
| public function delete($id) | |
| { | |
| $this->persistant->delete($id); | |
| return Redirect::to('/cart/list'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment