Domanda

I'm finished with this Laravel 4 Restfull tutorial here - http://code.tutsplus.com/tutorials/laravel-4-a-start-at-a-restful-api-updated--net-29785

What I want to do is to test the Restful link in the browser URL:

curl command:

$ curl -i --user firstuser:first_password -d 'url=hxxp://google.com&description=A Search Engine' www.lrvlapp.com/api/v1/url

this is running ok, and returning the json as expected:

{"error":false,"message":"URL created"}

browser URL: I try this:

www.lrvlapp.com/api/v1/url?url=hxxp://google.com&description=A Search Engine

no error or anything is given and no URL inserted into the database.

this is the UrlController

class UrlController extends \BaseController {

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    return 'Hello, API';
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    //
}

/**
 * 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->user_id = Auth::user()->id;

    // Validation and Filtering is sorely needed!!
    // Seriously, I'm a bad person for leaving that out.

    $url->save();

    return Response::json(array(
        'error' => false,
        'message' => 'URL created')
    );
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    //
}

}

www.lrvlapp.com is actually my virtual host I setup through the hosts file and apache Virtual Host

The actual location is at: c:\xampp\htdocs\lrvlapp\public

thank you for any answer

È stato utile?

Soluzione

When you are requesting through browser, you are issuing a GET request, which in turn invokes the index method. You need to submit a POST request to save the url. Then it will invoke the store method. It is the standard way of saving resources.

You can get more clarification in the doc: http://laravel.com/docs/controllers#resource-controllers

EDIT: If you want to make an ajax call (POST) using jquery in this case, you can call like below:

$.ajax({
            type: 'POST',
            url: 'www.lrvlapp.com/api/v1/url',
            data: { url: "http://google.com", description: "A Search Engine" },
            dataType: 'json',
            success: function(data) {
                 <do your actions, for example show some message>          
                 $('#div-id').html(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $('#div-id').html('Error: , ' + textStatus + ', ' + errorThrown);

            }
        });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top