4

What is the best way to remove duplicate codes from Laravel Controller? In my particular case, I have Blog Controller where are multiple functions for each of sub-pages (index page, about, contact, single post page...). In any of those functions I have some code which is repeated. Can I create a special function which then I could call into any of function?

class BlogController extends Controller {

    public function getIndex() {
        $blogs = Blog::orderBy('id', 'desc')->where('status', '1')->paginate(3);
        return view('index-page')->withBlogs($blogs);
    }

    public function getAbout() {
        $blogs = Blog::orderBy('id', 'desc')->where('status', '1')->paginate(3);
        return view('about-page')->withBlogs($blogs);
    }

}

And now, I want remove duplicate code with creating a special function (my code is only example, the real repeated code is much longer). Is that even possible? Is there some other way except creating another function? Maybe I can create something like function.php in Wordpress?

1
  • btw are these 'blogs' like a list of posts to use in a sidebar or footer? is that sidebar/footer its own partial? Commented Dec 14, 2017 at 21:30

3 Answers 3

4

You can create another function in Controller file and call it:

private function foo($view)
{
    $blogs = Blog::orderBy('id', 'desc')->where('status', '1')->paginate(3);
    return view($view)->withBlogs($blogs);
}

And then call it:

public function getIndex() {
    return $this->foo('index-page');
}

public function getAbout() {
    return $this->foo('about-page');
}

If you want to create a function that can be called everywhere, you can create a static function in a class. Ex:

public static function foo()
{
    return "foo";
}

and then call it:

NameOfClass::foo();
Sign up to request clarification or add additional context in comments.

Comments

1

You should move the data related logic into a repository or a model and get the data like this:

public function getIndex()
{
    return view('index-page', ['blogs' => $this->blog->paginateLatest()]);
}

And in the Blog model:

public function paginateLatest()
{
    return $this->latest('id')->where('status', 1)->paginate(3);
}

Comments

0

You have the option of moving some information to the route definition as well.

class SomeController ...
{
    public function showPage(Request $request)
    {
        return view(
            $request->route()->getAction('view'),
            ['blogs' => Blog::orderBy('id', 'desc')->where('status', '1')->paginate(3)]
        );
    }
}

Route::get('about', ['uses' => 'SomeController@showPage', 'view' => 'about-page']);
Route::get('contact', ['uses' => 'SomeController@showPage', 'view' => 'contact-page']);

Just throwing in an additional option that 'can' be done.

If you have a partial that needs these blog posts you can simplify this method by removing the query and moving it into a view composer:

public function showPage(Request $request)
{
    return view($request->route()->getAction('view'));
}

View::composer('some.partial.that.needs.those.blog.posts', function ($view) {
    $view->with(
        'blogs',
        Blog::orderBy('id', 'desc')->where('status', '1')->paginate(3)
    );
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.