I am developing an application with Laravel 5.2, it has to be implemented RESTFUL. It is also very easy to implement RESTful resources in Laravel. for example for getting all categories in json
format in routes
we just have to add
Route::resource('category', 'CategoryController');
and then in the CategoryController
we are going to have this for returning a JSON
object of all categories:
class CategoryController extends Controller
public function index()
{
$categories = Category::all();
return view('category.index',
['categories' => $categories]);
}
the the mydomain.com\category
will be mapped to the above function automatically
The mobile applications and web application all have to deal with an uniform interface. it is obvious that mobile app(s) will send the request to the above URl (ourdomain.com/category) and then they will parse the JSON
and display. but when it comes to web application I am getting a bit confused about how to implement the routes
and their correspondent functions in the Controllers
can I create the new functions in any format that I like?
for example: for displaying the categories in a web page is it recommended to create a new function in the same controller and call it for example displayAll
public function displayAll()
{
$categories = Category::all();
return view('category.index',
['categories' => $categories]);
}
and adding a route like
Route::get('category/all', 'CategoryController@displayAll()')
to the routes
file? or is there any specific convention to obey or is it a good practice to add those functions to the same Controller
or create a new Controller?