lundi 26 octobre 2015

How to do OOP and MVC properly PHP [on hold]

I've been programming with PHP for a few years and I still don't know how to implement OOP and MVC properly.

I'll put an example of a project that I'm currently developing in Laravel. I have an online library. I have a controller for each section. Something like Authors Controller, in that controller I have all the function related with that section. But this is opposite to the Single Responsibility Principle. I think that is not useful to create a controller for each task like show authors, another for create author, another for get books of the author, and that stuff. Now I'm trying to implement TDD and I'm not sure how to do it if my classes have no SRP. The problem with the section organization is that I don't "need" to use varibles in the class, I have enough with local variables.

Here is my showAuthor function in AuthorController

public function mostrarAutor($id)
{
    $autor = Autor::find($id);
    $libros=$autor->libros;
    $siguiendo=Auth::user()->autores_siguiendo()->where('autor_id','=',$autor->id)->first();
    $atribuciones=$autor->atribuciones()->get();
    $guardado=0;
    if ($siguiendo) $guardado=1;
    return $this->mostrarVista(View::make(Config::get('rv.autor'), array('autor' => $autor, 'libros' => $libros, 'guardado' => $guardado, 'atribuciones' => $atribuciones)));
}

I can't use polymorphism because I would need to instantiate classes and with the route system of MVC I never do that. I just

Route::get('/autor/{id}', 'AutorController@mostrarAutor')->where('id', '[0-9]+');

Like this I have more showAuthor functions like showAuthors, showAuthorsYouAreFollowing or ShowAuthorOfBook.

If the question isn't clear I'll say that I ask if someone can explain for AuthorsController which is a right way to implement OOP in Laravel.

Greetings and thank you very much.

Aucun commentaire:

Enregistrer un commentaire