当前位置: 首页 > 知识库问答 >
问题:

Laravel登录后不会重定向

公冶子琪
2023-03-14

每个人登录后,我一直无法使用Laravel重定向。连接工作,在我登录后,它重定向到一个空白页面,但如果我更改url路径,我可以访问不同的网页。我们将不胜感激!我正在使用LDAP进行连接,它正在工作。

请让我知道,如果有任何其他代码,我应该提供。

非常感谢。

(重定向IfAuthenticated.php

命名空间应用程序\ Http\中间件;

使用闭包;使用Illumb\Support\Facades\Auth;

类重定向验证{

protected $auth;
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/computers/create');
    }

    return $next($request);
}

}

我的路线

Route::group(['middleware' => ['web']], function () {

Route::auth();
Route::get('login', 'LoginController@index');
Route::post('login', 'LoginController@check_password');
Route::patch('computers/{inventories}', 'InventoriesController@update');
Route::get('computers/search', 'InventoriesController@search');
Route::resource('computers', 'InventoriesController');

}); 路由::get('/home','HomeController@index');

登录ontroller.php

    <?php namespace App\Http\Controllers;
/**
 * @class Login
 */
use App\User;
use Illuminate\Http\Request;
class Login extends Controller
{
    /**
     * Show the application dashboard to the user.
     *
     * @return Response
     */
    public function index()
    {
        return view('auth.login');
    }
    public function check_password(Request $req)

    {
        //die('has to stop here');
        $user = User::check_password($req);
//var_dump($user); die;
        if ($user)
        {

            return redirect('/computers/create');
        }
        else
    {

            return redirect('login')->with('message', 'Login Failed');
        }
    }
}

AuthController。php

    <?php

namespace App\Http\Controllers\Auth;


use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */

    protected $redirectTo = '/computers/create';
    protected $redirectAfterLogout = '/login';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */


    public function __construct()
    {
        //$this->auth = $auth;
        //$this->registrar = $registrar;

        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    //Using Ldap
//    protected function validator(array $data)
//    {
//        return Validator::make($data, [
//            'name' => 'required|max:255',
//            'email' => 'required|email|max:255|unique:users',
//            'password' => 'required|min:6|confirmed',
//        ]);
    //}
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    //Removed b/c LDAP is being usedcd
//    protected function create(array $data)
//    {
//        return User::create([
//            'name' => $data['name'],
//            'email' => $data['email'],
//            'password' => bcrypt($data['password']),
//        ]);
//    }
}

库存ontroller.php

  <?php

namespace  App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Inventory;
use App\Http\Requests\InventoryRequest;
class InventoriesController extends Controller
{
    public function __construct()
    {
        //$this->middleware('auth');  //does not allow users to login, redirects back to login when using LDAP credentials

    }
    public function index(Request $request)
    {
        $location = $request->input("building");
        if ($location != null) {
            $inventories = Inventory::where('building', $location)->get();
        }  else {
            $inventories = Inventory::all();
        }
        return view('computers.index', compact('inventories'));
    }
    public function show($inventories)
    {
        $inventories = Inventory::findOrFail($inventories);
        return view::make('computers.show')
            ->with('inventory', $inventories);
    }
    public function create(){
        //flash('Hello World', 'This is the message');
        return view('computers.create');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  inventory  $request
     * @return Response
     *
     */
    public function store(InventoryRequest $request)
    {
           Inventory::create($request->all());
        flash('Success!', 'Inventory Successfully Updated!');
//s
//        return redirect()->back();  //temporary
        return back();
    }
    public function edit($inventories)
    {
        $inventories = Inventory::findOrFail($inventories);
        return view('computers.edit', compact('inventories'));
    }
    public function update(InventoryRequest $request, Inventory $inventories){
        $inventories->update($request->all());
        flash('Success!', 'Inventory Successfully Updated!');
        return back();
    }
    public function search()
        {
            $search = \Request::get('q'); //<-- we use global request to get the param of URI
//            $search = Input::get('search');
            $inventories = Inventory::where('lastName','LIKE','%'.$search.'%')
               -> orwhere('firstName', 'LIKE','%'.$search.'%' )
                -> orwhere('department', 'LIKE','%'.$search.'%' )
                -> orwhere('building', 'LIKE','%'.$search.'%' )
                -> orwhere('room', 'LIKE','%'.$search.'%' )
                -> orwhere('manufacturer', 'LIKE','%'.$search.'%' )
                -> orwhere('device', 'LIKE','%'.$search.'%' )
                -> orwhere('model', 'LIKE','%'.$search.'%' )
                -> orwhere('tag', 'LIKE','%'.$search.'%' )
                -> orwhere('macAddress', 'LIKE','%'.$search.'%' )
                -> orwhere('status', 'LIKE','%'.$search.'%' )
                -> orwhere('comments', 'LIKE','%'.$search.'%' )
                ->get();
            return view('computers.search',compact('inventories'));
        }
}

共有1个答案

诸葛利
2023-03-14

检查ReDirectIfAuthenticated.php中间件。默认情况下,它应该如下所示:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }
}

确保返回的是好页面!!如果你没有那个中间件,可以看看Laravel的文档来创建一个!

 类似资料:
  • 我正在尝试实现一个功能,在登录后,用户会根据其角色重定向到URL。我已经设置了角色部分,但是在登录后立即测试用户的属性时遇到了问题。 我按照这里的说明创建了一个用户登录页面。我有一个看起来像这样的: 我的函数验证用户,但我不知道如何在登录后立即访问用户对象。这就是我目前拥有的: 当我第一次尝试使用管理员帐户登录时,我会进入路径,因为此时没有任何经过身份验证的用户对象。 在尝试登录后,但是得到了一个

  • 我在laravel中遇到了一个用户登录的问题,我可以正确地登录用户,但是问题是,在用户登录后,他被重定向到login POST方法,而不是home,我已经将

  • 我有一个页面,上面有一些内容和评论部分。评论只能由已登录的用户留下,因此我在页面中添加了一个登录表单供用户登录(仅当用户尚未登录时显示)。 我的问题是,当用户登录时,他们会被重定向回主页,而不是他们以前所在的页面。 我没有更改开箱即用设置的登录方法。 任何人都可以建议一个简单的方法来设置重定向url。我的想法是,能够将其设置为表单会很好。

  • 我在Laravel5.2中使用了php artisan函数。 我想重定向的客人登录页面,若客人点击链接,只有用户并没有客人。 我想在登录后将用户重定向到后面的页面。 我怎么能这么做?请详细展示一些文件名示例。 ///////编辑 路线 控制器 Kernel.php

  • 问题内容: 我正在使用Laravel Framework 5.4.10,并且正在使用常规身份验证 提供。我想保护整个应用程序,并在登录后将用户重定向到/ themes。 我有4个控制器:ForgotPasswordController.php,LoginController.php,RegisterController.php和ResetPasswordController.php。我已经将此行编

  • 我想在成功登录后为特定页面重定向用户。 我不希望用户在登录后导航到上次查看的页面。 我试过以下网址,但它显示我的错误。 错误: