路由资源更新和删除无法使用除存储 laravel 8 之外的多个角色

Posted

技术标签:

【中文标题】路由资源更新和删除无法使用除存储 laravel 8 之外的多个角色【英文标题】:route resource update and delete not working using multiple roles except store laravel 8 【发布时间】:2021-06-18 02:34:04 【问题描述】:

我想使用路由资源delete 我的管理产品。但是当我提交时,它会转到页面 404update 方法相同。但我的 URL id 正在显示...

这是 destroy 方法 http://127.0.0.1:8000/admin/1 上的 URL,但商店运行正常。它指向我的 URL 和数据库中的 stored

这是我在Admin/ProductController 资源上的store 方法:

public function store(Request $request)

    $request['user_id'] = auth()->user()->id;
    Product::create($request->all());

    return redirect()->back();

这是我在Admin/ProductController 资源上的destroy 方法:

 public function destroy($id)
 
     Product::where('id', $id->id)->delete();

     return redirect()->back();
 

这是我的表单操作store 方法。此表单工作正常:

<form method="post" action="route('admin.store')">
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>

这是我的表单操作delete。问题是当我提交时,它转到404 页面:

<form method="post" action=" route('admin.destroy', $product->id) ">
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>

这是我的路线useradmin

Route::get('/', function () 
    return view('welcome');
);

Auth::routes();

Route::group(['as' => 'user.', 'prefix' => 'users', 'namespace' => 'App\Http\Controllers\Users'], function() 
    Route::get('/', [App\Http\Controllers\Users\UsersController::class, 'index']);
    Route::resources(['/' => ProductController::class], ['except' => ['index']]);
);

Route::group(['middleware' => 'admin', 'as' => 'admin.', 'prefix' => 'admin', 'namespace' => 'App\Http\Controllers\Admin'], function() 
    Route::get('/', [App\Http\Controllers\Admin\AdminController::class, 'index']);
    Route::resources(['/' => ProductController::class], ['except' => ['index']]);
);

这是我的路线列表:

+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+------------+
| Domain | Method    | URI                    | Name             | Action                                                                 | Middleware |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+------------+
|        | GET|HEAD  | /                      |                  | Closure                                                                | web        |
|        | POST      | admin                  | admin.store      | App\Http\Controllers\Admin\ProductController@store                     | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin                  | admin.index      | App\Http\Controllers\Admin\ProductController@index                     | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin/create           | admin.create     | App\Http\Controllers\Admin\ProductController@create                    | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | DELETE    | admin/               | admin.destroy    | App\Http\Controllers\Admin\ProductController@destroy                   | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | PUT|PATCH | admin/               | admin.update     | App\Http\Controllers\Admin\ProductController@update                    | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin/               | admin.show       | App\Http\Controllers\Admin\ProductController@show                      | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | admin//edit          | admin.edit       | App\Http\Controllers\Admin\ProductController@edit                      | web        |
|        |           |                        |                  |                                                                        | admin      |
|        | GET|HEAD  | api/user               |                  | Closure                                                                | api        |
|        |           |                        |                  |                                                                        | auth:api   |
|        | POST      | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | GET|HEAD  | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | POST      | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web        |
|        | GET|HEAD  | password/confirm       | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | POST      | password/confirm       |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | POST      | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web        |
|        | GET|HEAD  | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web        |
|        | POST      | password/reset         | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web        |
|        | GET|HEAD  | password/reset/token | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web        |
|        | GET|HEAD  | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | POST      | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web        |
|        |           |                        |                  |                                                                        | guest      |
|        | GET|HEAD  | users                  | user.            | App\Http\Controllers\Users\UsersController@index                       | web        |
|        | POST      | users                  | user.store       | App\Http\Controllers\Users\ProductController@store                     | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | GET|HEAD  | users/create           | user.create      | App\Http\Controllers\Users\ProductController@create                    | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | DELETE    | users/               | user.destroy     | App\Http\Controllers\Users\ProductController@destroy                   | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | GET|HEAD  | users/               | user.show        | App\Http\Controllers\Users\ProductController@show                      | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | PUT|PATCH | users/               | user.update      | App\Http\Controllers\Users\ProductController@update                    | web        |
|        |           |                        |                  |                                                                        | auth       |
|        | GET|HEAD  | users//edit          | user.edit        | App\Http\Controllers\Users\ProductController@edit                      | web        |
|        |           |                        |                  |                                                                        | auth       |
+--------+-----------+------------------------+------------------+---------------------------------------

这是我的App\Http\Auth\LoginController

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller

    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    
        $this->middleware('guest')->except('logout');
    

    public function login(Request $request)
    
        $inputVal = $request->all();

        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if (auth()->attempt(['email' => $inputVal['email'], 'password' => $inputVal['password']])) 
            if (auth()->user()->role == 'admin') 
                return redirect()->route('admin.');
             else 
                return redirect()->route('user');
            
         else 
            return redirect()->route('login')
                ->with('error', 'Email & Password are incorrect.');
        
    

这是我的admin middleware

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Admin

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    
        if (auth()->user()->role === 'admin') 
            return $next($request);
        

        return redirect()->back();
    

我不知道这有什么问题,store 有效,但 updatedestroy 无效。我猜错误出现在routemiddleware

【问题讨论】:

【参考方案1】:

我认为您的问题是您没有告诉您的delete 表单使用DELETE 操作。由于 html,您不能使用 action="delete",因此您必须伪造它,而 Blade 允许您这样做。

所以试试这个:

<form method="post" action=" route('admin.destroy', $product->id) ">
    @csrf
    @method('DELETE')
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>

更多信息请访问Blade documentation。


应该在您的update 中应用相同的修复程序,您应该在更新表单的标记之后使用@method('PUT')

【讨论】:

您好,谢谢您的回答。我试过你的样品,但它仍然是 404 我认为你的路线有问题,看到一条路线有token,但你的adminuser,就像它缺少param。喜欢this example 但是当我不使用资源并且我使用这个 Route::delete('/',[App\Http\Controllers\Admin\AdminController::class,'destroy'])->name( '破坏');它工作正常 你必须使用我的blade 修复 (method) 并仍然修复你的路线。 但是您声明了一个resource,当您执行Route::delete 时它可以工作,因为您已经在说“去路由/删除”但是使用资源,您需要一个参数,即这就是为什么你看到admin/ 而不是admin/【参考方案2】:

如果您的方法是 post,那么您还必须添加 CSRF

<form method="post" action=" route('admin.destroy', $product->id) ">
     csrf_field() 
     method_field('delete') 
    <input type="text" name="category" class="form-control">
    <button type="submit" class="btn btn-primary">Save changes</button>
</form>

【讨论】:

以上是关于路由资源更新和删除无法使用除存储 laravel 8 之外的多个角色的主要内容,如果未能解决你的问题,请参考以下文章

laravel 命名资源无法正常工作并从头开始删除点

从 axios 删除 Laravel 资源路由

Laravel 5:无法发布路由资源

没有 id 的 Laravel 资源路由销毁(DELETE)

在 Laravel 中为资源控制器添加新方法

无法通过 PancakeSwap 路由器移除流动性。通过“写”合约成功添加,但无法删除LiquidityETH