简体   繁体   English

Laravel 9 UI 授权路由

[英]Laravel 9 UI Auth Routes

I installed Laravel UI using this tutorial https://www.itsolutionstuff.com/post/laravel-9-authentication-using-breeze-tutorialexample.html我使用本教程安装了 Laravel UI https://www.itsolutionstuff.com/post/laravel-9-authentication-using-breeze-tutorialexample.html

The login and register forms are there http://localhost:8000/login but after login if I go to my route ( http://localhost:8000/api/categories ) inside the middleware I am redirected to the home page.登录和注册 forms 在那里http://localhost:8000/login但是登录后如果我 go 到我的路线( http://localhost:8000/api/categories )我被重定向到主页。 If I have the route outside the middleware it works but without requiring a login.如果我有中间件之外的路由,它可以工作但不需要登录。

** Works ** (at least the 'Category' view shows) ** 作品 ** (至少“类别”视图显示)

    Route::controller(App\Http\Controllers\API\CategoryController::class)->group(function(){
        Route::get('categories', 'index')->name('categories.index')
    });

** Does Not work ** (redirects to home view) ** 不工作 ** (重定向到主视图)

Route::group(['middleware' => 'auth:api'], function(){
    Route::controller(App\Http\Controllers\API\CategoryController::class)->group(function(){
        Route::get('categories', 'index')->name('categories.index')
    });
});

** CategoryController** ** 类别控制器**

<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Category;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cat = $this->getCategories();
        return response()->json($cat);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cat = $this->getCategories();
        return view('create-category',compact('cat'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return Auth::user();
        $category = Category::firstOrCreate(
            ['name' => $role_name],
            ['guard_name' => 'api']
        );
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $cat = $this->getCategories($id);
//        $cat = Category::where('id', $id)->get()->keyBy('id');
        return response()->json($cat);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function getParentCategory($id) {
        $cat = Category::where('id', $id)->get()->keyBy('id');
        return $cat;
    }

    public function getChildCategory($id, $keyBy = null) {
//        return $keyBy;
        $cat = Category::where('parent_id', $id)->get()->keyBy('id');
        return $cat;
    }

    private function setKeyBy($collection, $name) {
        $collection = $collection->keyBy($name);
        return $collection;
    }

    public function getCategories($category_id = null) {
        $cat = Category::where('id', '>', 0);
        if(!is_null($category_id)) {
            $cat = $cat->where('id', $category_id)->get()->keyBy('id');
        } else {
            $cat = $cat->whereNull('parent_id')->get()->keyBy('id');

            foreach($cat as $catID=>$catArray) {
                $subCat = $this->getChildCategory($catID, 'id');
//                $subCat = $subCat->keyBy('id');
                if ($subCat->first()) {
                    $cat[$catID]['subcat'] = $subCat;
                }
            }
        }
        return $cat;
    }

    public function createCategoryForm() {
        $cat = $this->getCategories();
        return view('create-category',compact('cat'));
    }

    public function categoryDropown($child_id = null) {
        $cat = $this->getCategories();
    }

    public function categoryChildDropown($child_id) {
        $cat = Category::where('parent_id', $child_id)->get();
        return $cat;
    }
}

I have used Laravel for a while now but this is the first time creating an app from scratch with Auth.我已经使用 Laravel 一段时间了,但这是第一次使用 Auth 从头开始创建应用程序。 I do not know what I am missing.我不知道我错过了什么。 TIA TIA

You can try this:你可以试试这个:

$router->group(['middleware' => 'auth'], function() use ($router) {
    Route::controller(App\Http\Controllers\API\CategoryController::class)->group(function(){
        Route::get('categories', 'index')->name('categories.index')
    });
});

Thanks to Dream Bold for pointing me in the right direction.感谢Dream Bold为我指明了正确的方向。 I should have posted the web.php and api.php files.我应该发布 web.php 和 api.php 文件。 I found the answer here https://stackoverflow.com/a/35162656/1198563我在这里找到了答案https://stackoverflow.com/a/35162656/1198563
I had the route in the api.php Once I moved it into web.php, into the web middleware group and in the api middleware group it works.我在 api.php 中有路由 一旦我将它移动到 web.php,进入 web 中间件组和 api 中间件组它工作。

Route::group(['middleware' => 'web'], function () {
Route::auth();
// Moving here will ensure that sessions, csrf, etc. is included in all these routes
Route::group(['prefix' => 'api', 'middleware' => 'auth'], function () {
    Route::controller(App\Http\Controllers\API\CategoryController::class)->group(function () {
            Route::get('categories', 'index')->name('categories.index');//->middleware('auth');
        });
    });
});

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

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