I am somewhat new to OOP, although I know about interfaces and abstract classes a bit. I have a lot of resource controllers that are somewhat similar in the bigger scheme of things, they all look like the example below, the only main difference is the index and what I pass to the index view.
What I simply need to know is, can I OO things up a bit with my resource controllers? For example, create one "main" resource controller in which I simply pass the correct instances using an interface for example? I tried playing around with this but I got an error that the interface wasn't instantiable, so I had to bind it. But that means I could only bind an interface to a specific controller.
Any advice, tips and pointers will help me out :)
class NotesController extends Controller
{
public function index()
{
$notes = Note::all();
return view('notes.index', compact('notes'));
}
public function create()
{
return view('notes.create');
}
public function show(Note $note)
{
return view('notes.show', compact('note'));
}
public function edit(Note $note)
{
return view('notes.edit', compact('note'));
}
public function store(Request $request, User $user)
{
$user->getNotes()->create($request->all());
flash()->success('The note has been stored in the database.', 'Note created.');
return Redirect::route('notes.index');
}
public function update(Note $note, Request $request)
{
$note->update($request->all());
flash()->success('The note has been successfully edited.', 'Note edited.');
return Redirect::route('notes.index');
}
public function delete($slug)
{
Note::where('slug', '=', $slug)->delete();
return Redirect::to('notes');
}
}
Note: Totally my opinion!
I would keep them how you have them. It makes them easier to read and understand later. Also will save you time when you need to update one to do something different from the rest. We tried this in a project I worked on and while granted it wasn't the best implementation, it is still a pain point to this day.
Up to you though. I'm sure people have done that in a way that they love and works great. Just hasn't been the case in my experience. I doubt anyone would look at your code though and criticize you for not doing it.
In Case you need to bind different Model instanses then you may use Contextual Binding, for example, put the following code in AppServiceProvider's register() method:
$this->app->when('App\Http\Controllers\MainController')
->needs('Illuminate\Database\Eloquent\Model')
->give(function () {
$path = $this->app->request->path();
$resource = trim($path, '/');
if($pos = strpos($path, '/')) {
$resource = substr($path, 0, $pos);
}
$modelName = studly_case(str_singular($resource));
return app('App\\'.$modelName); // return the appropriate model
});
In your controller, use a __construct method to inject the model like this:
// Put the following at top of the class: use Illuminate\Database\Eloquent\Model;
public function __construct(Model $model)
{
$this->model = $model;
}
Then you may use something like this:
public function index()
{
// Extract this code in a separate method
$array = explode('\\', get_class($this->model));
$view = strtolower(end($array));
// Load the result
$result = $this->model->all();
return view($view.'.index', compact('result'));
}
Hope you got the idea so implement the rest of the methods.
Related
I am trying to apply a principle I read on Uncle Bob's book "Clean Code", which states that the optimal amount of parameters for a method is zero. I found this is very hard to accomplish, but anyways here I am trying it and I'd like your help to understand the best way to come closer to it in my code.
So far, I have a controller and a simple service to do the job I want. My controller is implemented as follows:
<?php
namespace App\Http\Controllers\Cms;
use App\Http\Controllers\Controller;
use App\Http\Requests\GroupRequest;
use App\Models\Group;
use App\Services\GroupsService;
use Illuminate\Http\Request;
class GroupsController extends Controller
{
protected $group_service;
public function __construct(GroupsService $group_service)
{
$this->group_service = $group_service;
}
public function index()
{
$groups = $this->group_service->listAll();
return view('cms.groups.index', compact('groups'));
}
public function store(GroupRequest $request)
{
$result = $this->group_service->createGroupWith($request->all());
return redirect()->back()->with('message', $result['msg']);
}
public function update(GroupRequest $request, Group $group)
{
$result = $this->group_service->updateAGroupWith($request->all(), $group);
return redirect()->back()->with('message', $result['msg']);
}
}
And bellow is my service
<?php
namespace App\Services;
use App\Models\Group;
use Illuminate\Support\Facades\DB;
class GroupsService
{
public function listAll()
{
$groups = Group::all();
return $groups;
}
public function createGroupWith($data)
{
try {
DB::beginTransaction();
$modules_id = array_pop($data);
$group = Group::create($data);
$group->modules()->attach($modules_id);
DB::commit();
return ['msg' => 'Grupo criado com sucesso!'];
} catch (\Throwable $error) {
DB::rollBack();
return ['msg' => $error->getMessage()];
}
}
public function updateAGroupWith($data, $group)
{
try {
DB::beginTransaction();
$modules_ids = array_pop($data);
$group->update($data);
$group->modules()->sync($modules_ids);
DB::commit();
return ['msg' => 'O grupo foi atualizado com sucesso!'];
} catch (\Throwable $error) {
DB::rollBack();
return ['msg' => $error->getMessage()];
}
}
}
As you can see, my service is instanciated by Laravel on the controller's __construct method and is used on the rest of my methods. The problem with this is that when storing or updating a register, I need to pass the data and (for the update) the gruop I want to update. This violates Uncle Bob's ideal of zero parameters and thus my question: is there a way to solve this?
I have come up with some ideas. The first one would be to continue instanciating the service as it is already done, but having properties such as group and data defined before calling the method I want. Something as implemented bellow. In this case the problem is that if I want to be really idealistic, I don't if I can keep accessing properties from an object and changing them, I believe I'd need to instanciate the object with it's state already defined anyways.
public function update(GroupRequest $request, Group $group)
{
$this->group_service->group = $group;
$this->group_service->data = $request->all();
$result = $this->group_service->updateAGroup();
return redirect()->back()->with('message', $result['msg']);
}
The second ideia would be to instanciate the service as I need it on each method:
public function update(GroupRequest $request, Group $group)
{
$service = new GroupService($data, $group);
$result = $service->updateAGroup();
return redirect()->back()->with('message', $result['msg']);
}
The only problem with the idea above is that in the fist method, the one for storing, I'd need to make the $group parameter for the $service = new GroupService($data, $group); statement optional. But I don't know if that's really a problem or a whim of mine.
And there's a third idea, but it seems way too much. It would be to make have a service per case (storing, updating and deleting maybe) and then I wouldn't have to worry with having optional parameters on the __construct method.
Anyways, I'd like to know your ideas, comments, and critiques too. Please take into consideration that I know this is very idealistic, but I'd really like to know how to come closer to it using with the patterns already defined on Laravel.
Thanks in advance.
I'm sure there is a common pattern for this kind of thing, and I'm struggling with search terms to find answers, so please bear with me if is this a dupe.
I have a few Classes in my app that create pretty standard Models that are stored in a relational database, eg;
// AtsType::name examples = 'XML', 'RSS', 'SOAP'
class AtsType extends Model
{
public function ats_instances()
{
return $this->hasMany('App\AtsInstance');
}
public function import()
{
}
}
What I need that import() method to do, however, somehow invokes a class/interface/contract/whatever based upon the actual model instance. So something like this;
AtsTypeRss::import()
AtsTypeXml::import()
AtsTypeSoap::import()
I'd like them to be standalone classes, in order to eventually use some artisan commands that will generate them for a developer, along with a data migration to create the new model names into the database.
I'm just unsure how to go about this.
You could try something like (as seen here), I've searched how to use variable in namespace :
class AtsType extends Model
{
protected $import_method = 'MyMethod';
public function ats_instances()
{
return $this->hasMany('App\AtsInstance');
}
public function import()
{
$string = $this->import_method;
$class = '\\controller\\' . $string;
$newObject = new $class();
}
}
I'm currently rebuilding my vanilla-PHP-App with Laravel and I have the following problem.
I have multiple database-tables, that represent word categories (noun, verb, adverb, ...). For each table I created a separate Model, a route::resource and a separate resource-Controller. For example:
NomenController.php
public function show($id)
{
$vocab = Nomen::find($id);
return view('glossarium.vocab_update', compact('vocab'));
}
and
VerbController.php
public function show($id)
{
$vocab = Verb::find($id);
return view('glossarium.vocab_update', compact('vocab'));
}
...which are essentially the same except the Model class.
I don't want to create a separate Controller for each model, that does exactly the same. What would be the most simple and elegant way to solve this?
Should I just create a VocabController.php and add a parameter for the Model-name like:
Route::resource('/vocab/{category}', 'VocabController');
and then add a constructor method in this controller like
public function __construct ($category) {
if ($category == 'nomen') {
$this->vocab = App\Nomen;
}
else if ($category == 'verb') {
$this->vocab = App\Verb;
}
}
I wonder if there is a simpler method to do that. Can I somehow do this with Route Model Binding?
Thanks in advance
Simply create a trait like this in App\Traits, (you can name it anything... Don't go with mine though... I feel its pretty lame... :P)
namespace App\Traits;
trait CommonControllerFunctions {
public function show($id) {
$modelObject = $this->model;
$model = $modelObject::find($id);
return view('glossarium.vocab_update', compact('model'));
}
}
and in your NomenController and VerbController, do this:
use App\Traits\CommonControllerFunctions;
class NomenController {
use CommonControllerFunctions;
protected $model = Nomen::class;
}
and
use App\Traits\CommonControllerFunctions;
class VerbController {
use CommonControllerFunctions;
protected $model = Verb::class;
}
Note: Please note that this example is just a work-around for your particular situation only... Everyone practices code differently, so this method might not be approved by all...
I think the simpliest way it to create only one controller, eg VocabController with methods nomen, verb and whatever you want.
Routes:
Route::get('/vocab/nomen/{nomen}', 'VocabController#item');
Route::get('/vocab/verb/{verb}', 'VocabController#item');
And the model binding:
Route::model('nomen', 'App\Nomen');
Route::model('verb', 'App\Varb');
Then your method shoud look like that:
public function item($item)
{
return view('glossarium.vocab_update', $item);
}
Keep in mind, that $item is already fetched model from the database.
Is there a way to fetch/manipulate a model agnostically in the AppController in order to avoid DRYness in the controllers of the application ? For example:
//AppController.php
public function find_all()
{
return $this->AppModel->find('all');
//I know this does not work but to give you the idea
}
And in children controllers of the app:
//FoosController.php
public function some_function()
{
$data = parent::find_all();
//List of Foo entities
}
Or:
//BarsController.php
public function some_other_function()
{
$data = parent::find_all();
//List of Bar entities
}
Is there a solution CakePHP can provide ? Or using reflection, maybe ?
Thank you for your help !
I figured out a way to achieve what I was looking for:
//AppController.php
public function find_all()
{
return $this->{$this->modelClass}->find('all');
}
NOTE: If you are performing repetitive CRUD operations without any real business logic or authorization involved (as I currently am), you can also use you these lines of code to persist entites without being aware of the model that is being handled in the AppController.
//AppController.php
if (!$this->{$this->modelClass}->save($this->request->data))
{
$validationErrors = $this->{$this->modelClass}->validationErrors;
//error logic here
}
else
{
//success logic here
}
I like MVC (a lot), and I am trying to teach myself a framework of MVC architecture in all the major web languages of today.
I am currently on CodeIgniter and PHP. I searched online for a way to make the same function behave different for a POST and GET but couldn't find anything. Does CodeIgniter have this feature?
If you've used Ruby On Rails or ASP.NET MVC you'll know what I'm talking about, in them frameworks we can do this:
[GET]
public ActionResult Edit(int Id)
{
// logic here for GET
}
[POST]
public ActionResult Edit(EntityX EX)
{
// logic here for POST
}
I am so used to this, that I am finding it hard wrapping my head around how to get the same smooth functionality without that useful ability.
Am I missing something? How can I achieve the same thing in CodeIgniter?
Thanks
Am I missing something? How can I achieve the same thing in
CodeIgniter?
if you want to learn how to truly approach MVC in PHP, you can learn it from Tom Butler articles
CodeIgniter implements Model-View-Presenter pattern, not MVC (even if it says so). If you want to implement a truly MVC-like application, you're on the wrong track.
In MVP:
View can be a class or a html template. View should never be aware of a Model.
View should never contain business logic
A Presenter is just a glue between a View and the Model. Its also responsible for generating output.
Note: A model should never be singular class. Its a number of classes. I'll call it as "Model" just for demonstration.
So it looks like as:
class Presenter
{
public function __construct(Model $model, View $view)
{
$this->model = $model;
$this->view = $view;
}
public function indexAction()
{
$data = $this->model->fetchSomeData();
$this->view->setSomeData($data);
echo $this->view->render();
}
}
In MVC:
Views are not HTML templates, but classes which are responsible for presentation logic
A View has direct access to a Model
A Controller should not generate a response, but change model variables (i.e assign vars from $_GET or $_POST
A controller should not be aware of a view
For example,
class View
{
public function __construct(Model $model)
{
$this->model = $model;
}
public function render()
{
ob_start();
$vars = $this->model->fetchSomeStuff();
extract($vars);
require('/template.phtml');
return ob_get_clean();
}
}
class Controller
{
public function __construct(Model $model)
{
$this->model = $model;
}
public function indexAction()
{
$this->model->setVars($_POST); // or something like that
}
}
$model = new Model();
$view = new View($model);
$controller = new Controller($model);
$controller->indexAction();
echo $view->render();
The parameters only allow you to retrieve GET variables. If you want to get the POST variables, you need to use the Input library which is automatically loaded by CodeIgniter:
$this->input->post('data');
So, in your case, it would be:
public function edit($id = -1)
{
if($id >= 0 && is_numeric($id))
{
// logic here for GET using $id
}
else if($id === -1 && $this->input->post('id') !== false)
{
// logic here for POST using $this->input->post('id')
}
}
Note that you can also use this library to obtain GET, COOKIE and SERVER variables:
$this->input->get('data');
$this->input->server('data');
$this->input->cookie('data');