Best design practice to implement routes and controllers for a RESTFul Laravel app

℡╲_俬逩灬. 提交于 2020-01-13 13:01:04

问题


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?

来源:https://stackoverflow.com/questions/37695419/best-design-practice-to-implement-routes-and-controllers-for-a-restful-laravel-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!