简体   繁体   English

Laravel模型路线不包装内部

[英]Laravel Model Route Not Binding Inside of Package

I have a test in my Laravel package that looks like this: 我的Laravel包中有一个测试,如下所示:

public function test_that_user_can_update_menu()
{
    $this->withoutExceptionHandling();

    $user = $this->signIn();
    $menu = factory(Menu::class)->create();

    $this->get("/menu/{$menu->id}/edit")
         ->assertStatus(200);

    $this->put(route('menu.update', $menu->id), $attributes = [
        'name' => 'Changed'
    ])->assertRedirect('/menu');

    $this->assertDatabaseHas('menus', $attributes);
}

The update method in my MenuController looks like this: 我的MenuController中的更新方法如下所示:

public function update(MenuRequest $request, Menu $menu)
{
    if ($menu->update($request->validated())) {
        return redirect('/menu');
    }

    abort(403);
}

If I dd out the $menu and actually find the Menu with id 1 it looks like this: 如果我输出$菜单并实际找到ID为1的菜单,它看起来像这样:

dd($menu, Menu::find(1));

I get this output: 我得到这个输出:

Package\Menu\Models\Menu^ {#1180
  #fillable: array:2 [
    0 => "name"
    1 => "primary"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
}
Package\Menu\Models\Menu^ {#1181
  #fillable: array:2 [
    0 => "name"
    1 => "primary"
  ]
  #connection: "sqlite"
  #table: "menus"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:5 [
    "id" => "1"
    "name" => "Main Menu"
    "primary" => "0"
    "created_at" => "2019-08-08 11:49:23"
    "updated_at" => "2019-08-08 11:49:23"
  ]
  #original: array:5 [
    "id" => "1"
    "name" => "Main Menu"
    "primary" => "0"
    "created_at" => "2019-08-08 11:49:23"
    "updated_at" => "2019-08-08 11:49:23"
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [
    0 => "*"
  ]
}

As you can see the Route Model Binding is not finding my menu with Id 1. Am I missing something here? 正如您所看到的,Route Model Binding没有找到Id 1的菜单。我在这里遗漏了什么吗?

Here's the route: 这是路线:

Route::middleware('auth')->group(function () {
    Route::resource('/menu', 'Package\Menu\Controllers\MenuController');
});

I don't understand why my model isn't binding. 我不明白为什么我的模型没有约束力。 Any thoughts? 有什么想法吗?

It's probably something silly. 这可能是愚蠢的事情。

在此输入图像描述

在此输入图像描述

Found the solution. 找到了解决方案。

In the package, it turns out there's a binding middleware that needs to be included. 在包中,事实证明需要包含一个绑定的中间件。 Here's what worked for me (at first): 这对我有用(起初):

Route::resource('/menu', 'Package\Menu\Controllers\MenuController')->middleware('bindings');

I noticed this by looking at the list of middleware . 我通过查看中间件列表注意到了这一点。

Then, after further testing, I realized that I could just use the web middleware group and set the auth to my resources inside of web as needed. 然后,在进一步测试之后,我意识到我可以使用Web中间件组并根据需要将auth设置为web内部的资源。 This also worked for me: 这也对我有用:

Route::group(['middleware' => ['web']], function () {
    Route::resource('/menu', 'Package\Menu\Controllers\MenuController')->middleware('auth');
});

I made a quick write-up . 我快速了一篇文章。

In Laravel applications, the web middleware is applied to the routes/web.php file. 在Laravel应用程序中,Web中间件应用于routes / web.php文件。 These routes are automatically assigned the web middleware group, which provides features like session state and CSRF protection. 这些路由会自动分配给Web中间件组,该组提供会话状态和CSRF保护等功能。 Because I neglected this, I was missing a lot of that default web middleware functionality. 因为我忽略了这一点,我错过了很多默认的Web中间件功能。 Hope this helps someone. 希望这有助于某人。

Usually i feel like there is problems with implicit binding. 通常我觉得隐式绑定存在问题。 If you do it explicit it should work. 如果你明确它,它应该工作。

In RouteServiceProvider.php , assuming your route parameter for the id is named menu . RouteServiceProvider.php ,假设您的id的路由参数名为menu

public function boot()
{
    parent::boot();

    Route::model('menu', Menu::class);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM