Hitender
Hitender

Reputation: 189

Laravel using separate controller for separate logic

I think we have separate controllers respective to separate logic or modules of our application, and i have also found that using a controller inside another controller is not a good practice.

Here i have facing a difficulty.

there are two controllers PagesController and PostsController

PagesController handle all pages related tasks.

class PagesController 
{

    public function index()
    {
         // method of our root request, get and show all posts
    }

    public function contactUs()
    {
        // show contact us page etc.
    }

}

PostsController handle all posts related tasks.

class PostsController
{

     public function getPosts() {} // get all posts from database
     public function deletePost($id) {} // delete a post
     public function editPost($id) {} // edit a post
}

Now post controller handle all posts specific tasks and pages controller handle all pages related tasks. The problem is that i want to use posts controller getPosts() method to get all posts and pass them to view. How can i use PostsController's getPosts() method inside our PagesController index() method.

One way is extends PostsController and use it. But what if we want to use another controller's method also.

Please provide me better way to do that.

Upvotes: 1

Views: 1630

Answers (5)

Rutvij Kothari
Rutvij Kothari

Reputation: 1285

I usually prefer Repository pattern to get task done.

Here's an Overview.

interface BaseMethodsForRepository {
    /**
     * @return mixed
     */
    public function get();

    //other base methods like store (handle create/update in common method) and delete.

}


class PostRepository implements BaseMethodsForRepository  {
    public function get() {
        return Post::all();
    }

    //Many more methods
}


class PagesRepository implements BaseMethodsForRepository  {
    public function get(){
        return Page::all();
    }
}

class PageController {
    private $postRepository
    public function __construct(PostRepository $postRepository) {
        $this->postRepository = $postRepository;
    }

    public function index(){
        //here you can use all public methods of PostRepository
        //usage
        $post = $this->postRepository->get(); 
    }
}

I found this useful and code is reusable.

Upvotes: 1

Jerico Pulvera
Jerico Pulvera

Reputation: 1042

https://www.youtube.com/watch?v=MF0jFKvS4SI

This talk is a bit advance about controllers but it is surely a good practice regarding controllers.

Upvotes: 2

Mahi
Mahi

Reputation: 105

Your controller should not have any logic. Create a service or create a method in Repository and move PostsController's getPosts()'s code into this method. And then call this new method in both PostsController and PageController.

The whole point of having Repository is for this purpose.

Upvotes: 1

Meera Tank
Meera Tank

Reputation: 719

You can use XyzController method in any controller by following way

use App\Http\Controllers\XyzController ;

class AnyController extends Controller {

    public function functionName()
    {
        $result = (new XyzController)->methodName();
        // this will call method of XyzController
    }
}

Hope this will help.

Upvotes: 1

RamAnji
RamAnji

Reputation: 470

better to create a trait...... How to use traits in Laravel 5.4.18?

Upvotes: 1

Related Questions