【问题标题】:Simplify function Laravel简化函数 Laravel
【发布时间】:2021-09-04 20:46:53
【问题描述】:

这是我创建新联系人的功能。

它工作正常,符合预期。

我想知道是否有更好的方法或最佳实践来简化和改进代码。

我只是想提高自己的技能并构建更高效的功能。

public function onSave()
    {

        $validator = Validator::make(
            [
                'firstname' => Input::get('firstname'),
                'lastname' => Input::get('lastname'),
                'phone' => Input::get('phone'),
                'email' => Input::get('email'),
                'line_1' => Input::get('line_1'),
                'line_2' => Input::get('line_2'),
                'line_3' => Input::get('line_3'),
                'city' => Input::get('city'),
                'state' => Input::get('state'),
                'postcode' => Input::get('postcode'),
                'organisation_id' => Input::get('organisation_id'),
            ],
            [
                'firstname' => 'required',
                'lastname' => 'required',
                'phone' => 'required',
                'email' => 'required|email',
                'line_1' => 'required',
                'line_2' => '',
                'line_3' => '',
                'city' => 'required',
                'state' => 'required',
                'postcode' => 'required',
                'organisation_id' => 'required',
            ]
        );

        if($validator->fails()){

            return Redirect::back()->withErrors($validator);

        } else {

        $contact = new Contact();

        $contact->firstname = Input::get('firstname');
        $contact->lastname = Input::get('lastname');
        $contact->phone = Input::get('phone');
        $contact->email = Input::get('email');
        $contact->line_1 = Input::get('line_1');
        $contact->line_2 = Input::get('line_2');
        $contact->line_3 = Input::get('line_3');
        $contact->city = Input::get('city');
        $contact->state = Input::get('state');
        $contact->postcode = Input::get('postcode');
        $contact->organisation_id = Input::get('organisation_id');

        $contact->save();

        //Flash::success('Form Submitted');

        return Redirect::to('/contacts/'.$contact->id);

        }

    }

【问题讨论】:

  • 在 Laravel 文档中查找表单请求。这应该是一个好的开始。然后在 Contact 模型上使字段可填充,并使用 $request->only() 方法填充它们
  • 如果您的代码有效并且您只是希望有人对其进行审核,请将其发布到Code Review
  • @MagnusEriksson 感谢您的链接.. 老实说以前不知道))
  • 不用担心。跟踪所有不同的 Stack Exchange 站点并不容易 :-)

标签: php laravel octobercms


【解决方案1】:

绝对可以有更好的代码:

  • 不要在控制器中使用Validator,而是使用Form Requests
  • 尝试使用$requestrequest() 外观而不是Input(因为这是标准)。
  • 当重定向到与实际不同的页面时,请尝试使用route('route name'),这样它对任何人都更具可读性,并且不太容易出现错误的 URL。

那么,让我向你展示最终代码(我将使用Laravel 8PHP 8):

首先创建Form Request(我将使用随机名称):

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreContact extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'firstname' => 'required',
            'lastname' => 'required',
            'phone' => 'required',
            'email' => 'required|email',
            'line_1' => 'required',
            'line_2' => '',
            'line_3' => '',
            'city' => 'required',
            'state' => 'required',
            'postcode' => 'required',
            'organisation_id' => 'required',
        ];
    }
}

那么,控制器应该如下所示:

use App\Http\Requests\StoreContact;

public function onSave(StoreContact $request)
{
    $contact = new Contact();

    $contact->firstname = $request->input('firstname');
    $contact->lastname = $request->input('lastname');
    $contact->phone = $request->input('phone');
    $contact->email = $request->input('email');
    $contact->line_1 = $request->input('line_1');
    $contact->line_2 = $request->input('line_2');
    $contact->line_3 = $request->input('line_3');
    $contact->city = $request->input('city');
    $contact->state = $request->input('state');
    $contact->postcode = $request->input('postcode');
    $contact->organisation_id = $request->input('organisation_id');

    $contact->save();

    return redirect(route('contacts.show', ['contact' => $contact->id]);
}

你的路线应该是这样的:

Route::get('/contacts/{contact}', [ContactController::class, 'show'])
    ->name('contact.show');

因此,当您使用Form Request 时,它将使用您提供的规则自动验证您的输入,如果任何规则失败,它将返回到先前的 URL (redirect()->back()) 并显示错误 (->withErrors())并带有输入 (->withInput())。

这意味着,如果您在控制器中(从方法的第一行开始),所有内容都经过验证和授权。你有绿灯。

【讨论】:

    猜你喜欢
    • 2013-01-11
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多