简体   繁体   中英

Laravel form best practice ? Hidden input OR Route parameter?

I am wondering what is the best practive between sending data through route parameters or hidden inputs in Laravel, or in general. For example, what is the best way in your opinion between :

Route path parameters

/*** Html form --> User and Account ID are sent as route parameters ***/

<form method="post" action="/associate/user/10/account/34">
    {{ csrf_field() }}
    <input type="submit" />
</form>

/*** Route ***/

Route::post('/associate/user/{user_id}/account/{account_id}', 'UserController@associate');

/*** Controller ***/

public function associate($user_id, $account_id) {

    $user = App\User::find($user_id);

    $account = App\Account::find($account_id);

    $user->account()->associate($account);

    $user->save();

    return back();
}

OR

Form hidden inputs

/*** Html form --> User and Account ID are sent as hidden inputs ***/

<form method="post" action="/associate/user-account">
    {{ csrf_field() }}
    <input type="hidden" name="user_id" value="10" />
    <input type="hidden" name="account_id" value="34" />
    <input type="submit" />
</form>

/*** Route ***/

Route::post('/associate/user-account', 'UserController@associate');

/*** Controller ***/

public function associate(Request $request) {

    $user = App\User::find($request->input('user_id'));

    $account = App\Account::find($request->input('account_id'));

    $user->account()->associate($account);

    $user->save();

    return back();
}

What do you think ?

Thanks a lot for your opinion.

My opinion on this is to use Route parameters where it makes sense.

It is not very clear in your example on what you're trying to achieve, ie having two pre-defined values as hidden inputs and just a submit button doesn't seem like a very practical real-world scenario.

Say, if you have a form to associate given user to an account, what you might be able to do is have the user_id as part of route parameters, and the account_id as a dropdown. This way your form might look something like this:

{{-- /user/{user_id}/associate-account --}}
<form action="{{ route('user.associate-account', [
        'user_id' => $userId 
    ]) }}>
   {{ csrf_field() }}

   <select name="account_id">...</select>

</form>

Your controller

public function associate(Request $request, $userId)
{         
}

The advantages of this:

1. You can consistently group routes, related to user object.

Example:

Route::group(['prefix' => 'user/{user_id}'], function ()
{
    Route::post('/associate-account', 'UserController@associate');
    Route::post('/upgrade-account',   'UserController@upgradeAccount');
    Route::post('/delete',            'UserController@delete');
});

As you can see it's very clear from the route that those three POST actions are all tied to given user_id

2. You can utilise Model Binding

Model Binding essentially turn your Model key, into actual Model instance, such that you can write your controller action as:

public function associate(Request $request, User $user)
{         
    // Here `user_id` has been converted into actual User model instance
    // by default Laravel also throws ModelNotFoundException when User 
    // can't be found with given `user_id`
    $user->account()->associate($request->get('account_id'));
}

As of Laravel 5.2 you can implicitly bind model to your route, prior to this you have to register them first.

This isn't so much a question about Laravel as it is about routing. It's really about how you want to design your app's routes, and what is logical to you, and those others who may work with it.

If you're going for a more RESTful approach, the former would be better (and what I tend to prefer). It makes your routes appear logically organized, which is better suited for making api's that others will potentially use.

Otherwise, if you don't intend to scale your app that far, and don't care to have an api with smexy url's, just go with the latter approach. I only recommend you be consistent with the one you pick.

Unless I missed something (which is possible), it just looks like two ways to skin a cat.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM