我使用本教程安装了Laravel UI https://www.itsolutionstuff.com/post/laravel-9-authentication-using-breeze-tutorialexample.html
登录和注册表单都在http://localhost:8000/login中,但是登录后,如果我进入中间件内部的路由(http://localhost:8000/api/categories),我会被重定向到主页。如果我有中间件外部的路由,它可以工作,但不需要登录。
工作(至少在“类别”视图中显示)
Route::controller(App\Http\Controllers\API\CategoryController::class)->group(function(){
Route::get('categories', 'index')->name('categories.index')
});
不工作(重定向到主视图)
Route::group(['middleware' => 'auth:api'], function(){
Route::controller(App\Http\Controllers\API\CategoryController::class)->group(function(){
Route::get('categories', 'index')->name('categories.index')
});
});
类别控制器
<?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;
}
}
我已经使用Laravel有一段时间了,但这是第一次用Auth从头开始创建应用程序。我不知道我错过了什么。TIA
2条答案
按热度按时间kse8i1jr1#
您可以尝试以下操作:
avkwfej42#
感谢Dream Bold为我指出了正确的方向。我应该发布web.php和api.php文件。我在这里找到了答案https://stackoverflow.com/a/35162656/1198563
我在api.php中有这个路径一旦我把它移到web.php中,移到web中间件组中,在api中间件组中它就可以工作了。