Support the ongoing development of Laravel.io →
Requests Architecture
Last updated 2 years ago.
0

This is an example of how this can work. If your api endpoint names can also be urls, then you can do something like this:


// Controller
public function index() {
    if ( Request::ajax() ) {
        return ModelName::all(); // Returning from a model will automatically create JSON repsonse
        // or
        return Response::json($someArrayOfData); // You can pass any collection or array and JSON will be returned
    } 

    // Normal request
    return View::make('yourViewName', $data);  // Request is a non-AJAX, so we will return a view, and pass the $data array to it
}

Last updated 2 years ago.
0

Hi!

Thank you very much.

But I have multiple questions based on your example:

  1. Will Request::ajax() be true if I access it via mobile app for example?
  2. Can I determine if it's an PUT, PUSH, GET or POST request?

Yeah and then I'd have the problem with versioning the API and Auth. Think OAuth would be the best for this since I want to make the API public some day.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sleepless sleepless Joined 20 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.