1
votes

I am working on a project on laravel 5.1, and i want to make it as a RESTFUL api so later we can use the same code and request data from mobile apps or other websites. The project is in its initial stages and i want to make it correct right from the beginning.

Suppose i have a simple route which is calling a the dashboard method on the AdminController. So after logging in it redirects the admin to the dashboard page with some data.

/******************** Laravel Project ***********************/
        //Routes.php
            Route::group(['middleware' => 'auth'], function () {
                Route::get('dashboard', 'AdminController@dashboard');
            });

       // AdminController
        public function index(){
            $data = 'Some Data';
            return view( 'superadmin.dashboard')->with('data', $data );
        }

Now i want to get the same data in a wordpress project. How will i use this api to just fetch the data variable (without the view) ? I dont want to create another method for that, is there any way i can use the same function to fetch data as a json?

I read in another forum that we can access all the data as a REST like this. But this is not working.

http://admin:[email protected]/dashboard

As always appreciate your help :)

1

1 Answers

0
votes

Personally, I would create an application that is the API. In your case this is your Laravel application.

Then I'd make HTTP requests to the API from Wordpress, or a mobile application.

I find returning JSON from the API is easier to work with. Laravel makes this easy:

return Response::json(array(
    'username' => 'superadmin',
    'role' => 'admin',
    'friends' => array(
      '2345',
      '884'
    )
));

Also, don't send your username and password like that. HTTP auth is insecure. http://adrianotto.com/2013/02/why-http-basic-auth-is-bad/

I tend to use OAuth to secure my APIs.