dshdsh2016 2018-02-16 17:10
浏览 26

仅在先前在创建新帖子链接中单击用户时,才会在注册后将用户重定向到创建帖子页面

I have a home page that has a link "Create Post" to create a new post (Create Post).

When this link is clicked, if the user is authenticated he should be allowed to access this create post page.

But when the user dont have an account and click in "Create Post" he is redirected to the login page and in this login page there is a link to the register page. If the user click in this register link will be redirected to the register page.

In this page he need to enter the name, email and password and submit the form to create an account, and because he previously cicked in the "Create Post" link he should be redirected to the create post page but its not working properly:

Error:

The user just should be redirected to the create post page after register if he previously clicked in the create new post link, otherwise he should be redirected to the homepage after register. But the user is being always redirected to the create post page after he registers.

Do you know where is the error?

RegisterController

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Http\Controllers\Auth\Request as AuthRequest;

class RegisterController extends Controller
{


    use RegistersUsers;

    protected $redirectTo = '/home';


    public function __construct()
    {
        $this->middleware('guest');
    }


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),

        ]);
    }

   protected function registered($request, $user)
    {
     return redirect('(/users/createPost');
    }
}

Routes:

Route::group(['prefix' => 'users', 'middleware' => 'auth'], function(){
    Route::post('/post/store', [
        'uses' => 'PostController@store',
        'as'   => 'post.store'
    ]);

    Route::get('/createPost', [
        'uses' => 'PostController@create',
        'as'   => 'post.create'
    ]);
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
  • 写回答

1条回答 默认 最新

  • dougou5844 2018-02-16 17:17
    关注

    You can do this change in the __construct method of RegisterController:

    public function __construct()
    {
        $this->middleware('guest');
        $this->redirectTo = str_replace(url('/'), '', url()->previous();
    }
    

    So that any time you try accessing a page that requires authentication, you will be redirected to the previous page you were trying to access without not being authenticated.

    评论

报告相同问题?