php 从 Laravel 控制器调用私有函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22404334/
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-08-25 04:54:26  来源:igfitidea点击:

Calling private function from laravel controller

phplaravellaravel-4

提问by babbaggeii

I'm trying to extract some code to a private function inside a controller in order to tidy it up a bit, but the function seems not to run.

我试图将一些代码提取到控制器内的私有函数中,以便稍微整理一下,但该函数似乎没有运行。

Route:

路线:

Route::resource('posts', 'PostsController');

When I GET the following URL:

当我获取以下 URL 时:

/posts?page=2&posts_per_page=3&published=0

It ignores the page and posts_per_page variables (i.e. it just returns the first 10 unpublished results) But when I had this code inside the indexmethod it worked.

它忽略 page 和 posts_per_page 变量(即它只返回前 10 个未发布的结果)但是当我在index方法中使用此代码时,它可以工作。

public function index()
{

    // set defaults for page number and posts per page
    $page = 1;
    $postsPerPage = 10;
    $published = 1;

    $this->getPagesAndPostsPerPage($page, $postsPerPage);

    // Get published or not
    // Not published = 0
    // Published = 1

    if ( Input::has('published') )
    {
        $published = Input::get('published');
    }

    // return paginated results
    $skip = ($page - 1) * $postsPerPage;
    $posts = Post::where('published', '=', $published)
                        ->orderBy('published_date', 'desc')
                        ->skip($skip)
                        ->take($postsPerPage)
                        ->get();

    return Response::json([
        'data' => $this->transformCollection($posts)
    ], 200);
}

// Get pages and posts per page
private function getPagesAndPostsPerPage($page, $postsPerPage)
{

    // if posts per page and page are defined
    if ( Input::has('posts_per_page') && Input::has('page') )
    {
        // get inputs
        $postsPerPage = Input::get('posts_per_page');
        $page = Input::get('page');
    }

    // else if just page is defined
    elseif ( Input::has('page') ) 
    {
        $page = Input::get('page');
    }
}

Can anyone see what I'm doing wrong?

谁能看到我做错了什么?

回答by Steve Bauman

You're not returning anything from $this->getPagesAndPostsPerPage($page, $postsPerPage);

你没有返回任何东西 $this->getPagesAndPostsPerPage($page, $postsPerPage);

Variables defined inside a function are function specific, which means you need to pass function variables back and forth if you need to grab their updated value. Either that, or use class variables (properties) so you can access the updated value from anywhere in your code.

函数内部定义的变量是特定于函数的,这意味着如果需要获取它们的更新值,则需要来回传递函数变量。要么,要么使用类变量(属性),以便您可以从代码中的任何位置访问更新的值。

Your controller:

您的控制器:

public function index()
{
    // set defaults for page number and posts per page
    $page = 1;
    $postsPerPage = 10;
    $published = 1;

    // Get the return value:
    $pagesAndPostsPerPage = $this->getPagesAndPostsPerPage($page, $postsPerPage);

Your getPagesandPostsPerPage()function:

你的getPagesandPostsPerPage()功能:

private function getPagesAndPostsPerPage($page, $postsPerPage){
    //Grab pages and posts per page here

    //Create a new array and set the values
    $pagesAndPostsPerPage = array(
        'page' =>$page,
        'postsPerPage'=>$postsPerPage,
    );
    //Return the array
    return $pagesAndPostsPerPage;
}

If you need more explanation let me know! I'd be glad to help you out further.

如果您需要更多解释,请告诉我!我很乐意为您提供进一步的帮助。

EDIT:

编辑:

Forgot to mention, you can also pass the variable as a reference to the function if you don't want to provide a return value to functions using the ampersand (&) operator to just modify them.

忘了提一下,如果您不想使用与号 ( &) 运算符仅向函数提供返回值来修改它们,您也可以将变量作为对函数的引用传递。

http://php.net/manual/en/language.references.pass.php

http://php.net/manual/en/language.references.pass.php

Example:

例子:

function addOne(&$number)
{
    $number++;

    // Notice no return statement
}

$number = 1;

addOne($number);

echo $number; // Returns 2