laravel 如何将变量传递给注册视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42336115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:22:00  来源:igfitidea点击:

How can I pass variable to register view?

laravellaravel-5.3

提问by Steve

I have used php artisan make:authit generates the register view and /register route. But I need to pass a variable to this register view:

我已经使用php artisan make:auth它生成了注册视图和 /register 路由。但是我需要将一个变量传递给这个寄存器视图:

<label>Region:</label>
<select  name="region" id="region" class="form-control" >
<option>--Select a Region--</option>
    @foreach($region as $reg)
        <option value="{{$reg->region_id}}">{{$reg->region_name}}</option>
    @endforeach                
</select><br>

Something like:

就像是:

public function register()
{
   $region=Region::all();
   return view('auth.register')->with('region',$region);
}

But where is this kind of method?

但是这种方法在哪里呢?

回答by Saravanan Sampathkumar

You can achieve that in two ways.

您可以通过两种方式实现这一目标。

Overriding RegistersUsers

覆盖注册用户

Laravel's default auth uses RegistersUsers trait on RegisterController to render view. What you can do is simply override the function found on Illuminate\Foundation\Auth\RegistersUsers on RegisterController like the following

Laravel 的默认身份验证使用 RegisterController 上的 RegistersUsers 特性来呈现视图。您可以做的是简单地覆盖在 RegisterController 上的 Illuminate\Foundation\Auth\RegistersUsers 上找到的函数,如下所示

/**
 * Show the application registration form.
 *
 * @return \Illuminate\Http\Response
 */
public function showRegistrationForm()
{
    $region=Region::all();
    return view('auth.register', compact('region'));
}

Now above code will over ride the trait and use the showRegistrationForm from controller.

现在上面的代码将覆盖特征并使用来自控制器的 showRegistrationForm 。

Modifying Routes

修改路线

When you do php artisan make:auth, it will add the Auth::routes()to your web.php file. Remove that and add the following,

当你这样做时php artisan make:auth,它会添加Auth::routes()到你的 web.php 文件中。删除它并添加以下内容,

// Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');

Now on route for register, modify the RegisterController@showRegistrationForm to RegisterController@register.

现在在注册路由上,将 RegisterController@showRegistrationForm 修改为 RegisterController@register。

But do not use simply register. Instead use like getRegisterForm. Because register function handles the post registration logic.

但不要使用简单的注册。而是使用像 getRegisterForm。因为 register 函数处理注册后逻辑。

回答by Maniruzzaman Akash

In Laravel Auth:it uses showRegistrationForm()method to create registration page. So we need to override that method.

Laravel Auth:它使用的showRegistrationForm()方法来创建注册页面。所以我们需要重写那个方法。

Just Override the showRegistrationForm()method

只需覆盖该showRegistrationForm()方法

public function showRegistrationForm() {
    $data = [1, 2, 3, 4];
    return view ('auth.register')->withData($data);
}

And capture that data in registration page using $data...

并使用$data...在注册页面中捕获该数据

回答by ettdro

This function should be created manually in the RegisterController.phpin theapp/http/controllers/auth/folder. From this controller you can create any function but watch out for duplicate function used by the trait RegistersUsers.php.

此功能应RegisterController.phpapp/http/controllers/auth/文件夹中手动创建。从这个控制器你可以创建任何函数,但要注意 trait 使用的重复函数RegistersUsers.php

Your function could also be written like that:

你的函数也可以这样写:

public function register()
{
    $region = Region::all();
    return view('auth.register', compact('region'));
    // This will send the $region variable to the view
}

Hope it helps!

希望能帮助到你!

回答by ashanrupasinghe

write showLoginForm() function in your RegisterController.php file, function name is showLoginForm()and it will overwrite laravel default showLoginForm() function.

在 RegisterController.php 文件中写入 showLoginForm() 函数,函数名是showLoginForm(),它将覆盖 Laravel 默认的 showLoginForm() 函数。

public function showLoginForm()
{
   $region=Region::all();
   return view('auth.register')->with('region',$region);
}