Laravel 控制器 - 在另一个函数中调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47821738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:06:37  来源:igfitidea点击:

Laravel Controller - call function inside another function

phplaravel

提问by CRooY3

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?

从 Laravel 控制器中删除重复代码的最佳方法是什么?在我的特殊情况下,我有博客控制器,其中每个子页面(索引页面、关于、联系人、单个帖子页面......)都有多个功能。在这些函数中的任何一个中,我都有一些重复的代码。我可以创建一个特殊的函数,然后我可以调用任何函数吗?

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?

现在,我想通过创建一个特殊函数来删除重复代码(我的代码只是示例,真正的重复代码要长得多)。这甚至可能吗?除了创建另一个函数之外,还有其他方法吗?也许我可以在 Wordpress 中创建类似 function.php 的东西?

回答by Laerte

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

您可以在 Controller 文件中创建另一个函数并调用它:

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 staticfunction in a class. Ex:

如果你想创建一个可以随处调用的static函数,你可以在一个类中创建一个函数。前任:

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

and then call it:

然后调用它:

NameOfClass::foo();

回答by Alexey Mezenin

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

您应该将与数据相关的逻辑移动到存储库或模型中,并像这样获取数据:

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

And in the Blogmodel:

Blog模型中:

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

回答by lagbox

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