简体   繁体   中英

Laravel Controller - call function inside another function

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?

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();

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);
}

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)
    );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM