我遵循了本教程:顶顶
我想为这种类型的音箱应用程序创建一个API:
/ api / v1 / Apps -> list of apps
/ api / v1 / Apps / 1 /category -> list of category of app 1
/ api / v1 / Apps / 1/category/1/sounds -> list of components of category 1 of app 1你有教程来做这个吗?或者如何修改我的路由文件?我有三个控制器吗?
我创建了模型和控制器:
class App extends Model{
protected $fillable = ['title'];
}
class Category extends Model{
protected $fillable = ['name'];
}管制员:
<?php
namespace App\Http\Controllers;
use App\App;
use Illuminate\Http\Request;
class AppController extends Controller{
public function index(){
return App::all();
}
public function show($id){
return App::find($id);
}
}
<?php
namespace App\Http\Controllers;
use App\Category;
use App\App;
use Illuminate\Http\Request;
class CategoryController extends Controller{
public function index(App $app){
return App::find($app);
}
public function show(App $app, Category $category){
//
}
}我创建了迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAppsTable extends Migration{
public function up(){
Schema::create('apps', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration{
public function up(){
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('app_id')->unsigned();
$table->timestamps();
});
Schema::table('categories', function($table) {
$table->foreign('app_id')->references('id')->on('apps');
});
}
}我的路由/api.php文件:
Route::group(['prefix' => 'v1'], function() {
Route::resource('App', 'AppController', ['only' => ['index', 'show']]);
Route::resource('app.category', 'CategoryController', ['only' =>['index', 'show']]);
});所以我调用url : /api/v1/App
结果:{"id":1,“标题”:“电影”,“created_at”:空,“updated_at”:空},{"id":2,“标题”:“足球”,"created_at":null,"updated_at":null}
但是当我调用url: /api/v1/App/2/类别时,我不明白它是如何工作的。
在CategoryController里。
发布于 2017-09-21 21:08:38
您可以为此使用laravel的嵌套资源路由,是的,我将为每个实体类型提供一个足智多谋的控制器。在生成控制器时指定--parent标志,以便将相应的模型注入控制器方法。
php artisan make:controller --resource --model Category CategoryController
php artisan make:controller --resource --parent Category --model App AppController编辑:如果您没有设置身份验证,那么您可能只想公开您的index和show方法。
演示存储库:https://github.com/MyTeamName/appstore4233593
路线
Route::group(['prefix' => 'v1'], function() {
Route::resource('categories', 'CategoryController', ['only' => ['index', 'show']]);
Route::resource('categories.apps', 'AppController', ['only' => ['index', 'show']]);
});+----------+-----------------------------------------+-----------------------+---------+-----------------------+-----------------------------------------------+------------+ \GET/HEAD\api/v1/类别/ categories.index@app\Http\管制员\CategoryController@index}/apps\ categories.apps.index \index}apps/V1/index/{categories.apps.index} categories.apps应用程序\Http\管制员\AppController@ .show @show_ api \.show?
模型
class Category extends Model
{
protected $fillable = [
'name',
];
public function apps()
{
return $this->hasMany(App::class);
}
}
class App extends Model
{
protected $fillable = [
'title',
];
public function category()
{
return $this->belongsTo(Category::class);
}
}迁徙
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name')->unique();
});
Schema::create('apps', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('title');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')
->references('id')
->on('categories');
});控制器
class CategoryController extends Controller
{
public function index()
{
return Category::all();
}
public function show(Category $category)
{
return $category;
}
}
class AppController extends Controller
{
public function index(Category $category)
{
return $category->apps()->get();
}
public function show(Category $category, App $app)
{
return $app;
}
}https://stackoverflow.com/questions/46352758
复制相似问题