如何解决Laravel 包登录重定向到带有 302 状态代码的起始页
目标
问题
按登录按钮重定向到起始页,在控制台中我得到一个 302 状态代码。这就是登录按钮的路径:<a href="{{ route('login') }}">Login</a>
。它在 web.PHP
中定义如下:Route::get('login',[LoginController::class,'login'])->name('login')
。当我在地址栏中尝试使用:127.0.0.1:8000/login
时,也会发生同样的情况。
说明
这是我第一次尝试 Laravel 打包开发。因此,我使用 Laravel 8 创建了一个新的 Laravel 应用程序。使用 PHP artisan ui bootstrap --auth
我创建了身份验证脚手架,现在尝试使其成为 Laravel 包。我关注了this instructions。
您可以找到项目 here 的整个分支。
提前感谢您的时间。
到目前为止,这就是包的结构:
路线
<?PHP
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Route;
use Leonp5\Limplecms\Http\Controllers\Admin\PagesController;
use Leonp5\Limplecms\Http\Controllers\Auth\LoginController;
// Set language
Route::get('locale/{locale}',function ($locale) {
Session::put('locale',$locale);
return redirect()->back();
});
Route::get('/',function () {
return view('limplecms::welcome');
});
Route::get('login','login'])->name('login');
Route::post('login','login']);
Route::post('logout','logout'])->name('logout');
Route::get('/home',[Leonp5\Limplecms\Http\Controllers\HomeController::class,'index'])->name('home');
Route::resource(config('limplecms.admin_index_pages_url'),PagesController::class)->middleware('can:admin');
包的依赖提供者
<?PHP
namespace Leonp5\Limplecms;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
class LimpleCmsProvider extends ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/config.PHP','limplecms');
$this->mergeConfigFrom(__DIR__ . '/../config/auth.PHP','limplecms');
}
public function boot(GateContract $gate)
{
$this->registerRoutes();
$this->loadViewsFrom(__DIR__ . '/../resources/views','limplecms');
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../dist' => public_path('limplecms'),],'assets');
$this->publishes([
__DIR__ . '/../config/config.PHP' => config_path('limplecms.PHP'),'config');
$this->publishes([
__DIR__ . '/../resources/views' => resource_path('views/vendor/limplecms'),'views');
}
$gate->define("admin",function ($user) {
return $user->hasRole("admin");
});
$gate->define("editor",function ($user) {
return $user->hasRole("editor");
});
}
protected function registerRoutes()
{
Route::group($this->routeConfiguration(),function () {
$this->loadRoutesFrom(__DIR__ . '/../routes/web.PHP');
});
}
protected function routeConfiguration()
{
return [
// 'prefix' => config('limplecms.prefix'),'middleware' => config('limplecms.middleware'),];
}
}
登录控制器
(登录/注销方法在来自 Laravel 的特征 AuthenticatesUsers
中定义,我没有在那里更改任何内容)
<?PHP
namespace Leonp5\Limplecms\Http\Controllers\Auth;
use Leonp5\Limplecms\Http\Controllers\Controller;
// use App\Providers\RouteServiceProvider;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
// protected $redirectTo = RouteServiceProvider::HOME;
// commenting out this line and add a specific redirectTo function for a custom redirect
// protected $redirectTo = RouteServiceProvider::HOME;
// needs to bring in Auth Facade
/**
*
* @return mixed
* @throws BindingResolutionException
*/
public function redirectTo()
{
$user = Auth::user();
if ($user->hasRole("admin")) {
$this->redirectTo = route('pages.index');
} else {
$this->redirectTo = route("home");
}
return $this->redirectTo;
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
$login = request()->input('identity');
$field = filter_var($login,FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$field => $login]);
return $field;
}
/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function validateLogin(Request $request)
{
$messages = [
'identity.required' => 'Email or username cannot be empty','password.required' => 'Password cannot be empty',];
$request->validate([
'identity' => 'required|string','password' => 'required|string',$messages);
}
}
HomeController
<?PHP
namespace Leonp5\Limplecms\Http\Controllers;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('limplecms::home');
}
}
'providers' => [
'users' => [
'driver' => 'eloquent','model' => Leonp5\Limplecms\Models\User::class,
<?PHP
return [
'admin_index_pages_url' => '/admin/pages','prefix' => 'cms','middleware' => ['web'],];
标准脚手架迎宾刀片
(不要认为问题出在这里,因为在浏览器地址栏中输入 127.0.0.1:8000/login
也会重定向。但谁知道呢...)
<div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center sm:pt-0">
@if (Route::has('login'))
<div class="hidden fixed top-0 right-0 px-6 py-4 sm:block">
@auth
<a href="{{ url('/home') }}" class="text-sm text-gray-700 underline">Home</a>
@else
<a href="{{ route('login') }}" class="text-sm text-gray-700 underline">Login</a>
@if (Route::has('register'))
<a href="{{ route('register') }}" class="ml-4 text-sm text-gray-700 underline">Register</a>
@endif
@endif
</div>
@endif
解决方法
感谢@Adam P. 评论,我尝试在 LoginController
中定义路由以显示登录表单并且有效。所以在 LoginController
里面我添加了这个方法:
public function show()
{
return view('limplecms::auth.login');
}
在 web.php
内部,我更改了定义路由的位置Route::get('login',[LoginController::class,'login'])->name('login');
到Route::get('login','show'])->name('login');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。