将GET输入添加到Laravel中的资源控制器

时间:2014-06-26 19:52:57

标签: php laravel laravel-4 controller

使用Laravel 4创建“Read-it-Later”应用程序仅用于测试目的。 我可以使用以下curl命令将URL和描述成功存储到我的应用程序中:

curl -d 'url=http://testsite.com&description=For Testing' readitlater.local/api/v1/url

我有兴趣使用GET完成同样的事情,但是通过在URL中传递我的变量(例如 readitlater.local / api / v1 / url?url = testsite.com?description = For%20Testing < /强>)

这是我的UrlController细分:

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    $url = new Url;
    $url->url = Request::get('url');
    $url->description = Request::get('description');

    $url->save();

    return Response::json(array(
        'error' => false,
        'urls' => $urls->toArray()),
        200
    );
}

这是我的Url模型:

<?php

class Url extends Eloquent {

    protected $table = 'urls';

}

我阅读了关于输入类型的Laravel文档,但我不确定如何将其应用于我当前的控制器:http://laravel.com/docs/requests#basic-input

任何提示?

1 个答案:

答案 0 :(得分:1)

您没有应用正确链接的内容...使用Input::get()从GET或POST获取任何内容,使用Request类获取有关当前请求的信息。你在寻找这样的东西吗?

        public function store()
        {
            $url = new Url; // I guess this is your Model
            $url->url = Request::url();
            $url->description = Input::get('description');
            $url->save();

            return Response::json(array(
                'error' => false,
                'urls' => Url::find($url->id)->toArray(), 
    /* Not sure about this. You want info for the current url? 
    (you already have them...no need to query the DB) or you want ALL the urls? 
    In this case, use Url::all()->toArray()
    */
                200
            );
        }