doujiaoben28596 2014-03-14 11:56
浏览 155

从laravel控制器调用私有功能

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:

/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 index method it worked.

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?

  • 写回答

1条回答 默认 最新

  • dqroc48068 2014-03-14 14:50
    关注

    You're not returning anything from $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;
    
    $pagesAndPostsPerPage = $this->getPagesAndPostsPerPage($page, $postsPerPage);
    

    Your getPagesandPostsPerPage() function:

    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

    Example:

    function addOne(&$number)
    {
        $number++;
    
        // Notice no return statement
    }
    
    $number = 1;
    
    addOne($number);
    
    echo $number; // Returns 2
    
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏