【问题标题】:Passing variables to replace wildcard on laravel Route?传递变量以替换laravel Route上的通配符?
【发布时间】:2018-01-13 05:01:48
【问题描述】:

我正在练习 laravel ,关于 laracast 的第 11 课基础课,想知道我是否从下面的表单页面创建实体

<html> blahblah..
..
<form method="post" action="{{ Route('customModel.store') }}">
    forms.. many forms..
</form>
..
</html>

当我提交此表单时,数据将通过路由器。

Route::post('/customModel', [
    'as'=>'customModel.store',
    'uses'=>'CustomModelController@store
]);

CustomModelController 有一个名为 store 的方法,问题就在这里..

public function store( Request $request )
{
    $CustomModel = CustomModel::create([
        'name' => Request('name'),
        'desc' => Request('desc')
    ]);

    // Here is the PROBLEMMMM..
    return redirect('/field/'. $CustomModel->id );
}

感觉真的……嗯……怪怪的,直接用redirect函数,直接附加一些变量来填充通配符值。

还有其他方法可以代替redirect()吗?

Route 或其他人一样做某事?

【问题讨论】:

  • 是你不喜欢的辅助函数还是连接通配符值?
  • 你可以做return app()-&gt;make("ControllerYouWant")-&gt;methodYouWant($CustomModel-&gt;id),但仅仅因为你可以并不意味着你应该
  • m.. 我认为连接值是我不喜欢的.. 因为感觉就像,存在一种更好更简单的方法来做到这一点。它存在吗?

标签: laravel redirect routes


【解决方案1】:

你也可以使用Illuminate\Routing\Redirector类的route()方法作为:

return redirect()->route('route_name', ['id' => $id]);

【讨论】:

    猜你喜欢
    • 2016-07-19
    • 2015-09-15
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    相关资源
    最近更新 更多