将页面 URL 参数传递给 Laravel 5.2 中的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37013941/
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-08 17:20:20  来源:igfitidea点击:

Passing page URL parameter to controller in Laravel 5.2

laravelurlcontrollerrouteslaravel-5.2

提问by Abolfazl

In my application I have a page it called index.blade, with route /index. In its URL, it has some getparameter like ?orderand ?type.

在我的应用程序中,我有一个名为index.bladeroute的页面/index。在它的 URL 中,它有一些get参数,比如?order?type

I want to pass these $_getparameter to my route controller action, query from DB and pass its result data to the index page. What should I do?

我想将这些$_get参数传递给我的路由控制器操作,从数据库查询并将其结果数据传递给索引页面。我该怎么办?

回答by Achraf Khouadja

If you want to access the data sent from getor postrequest use

如果您想访问从发送的数据getpost请求使用

public function store(Request $request)
{
    $order = $request->input('order');
    $type = $request->input('type');
    return view('whatever')->with('order', $order)->with('type', $type);
}

you can also use wildcards.

您还可以使用通配符。

Exemple link

示例链接

website.dev/user/potato

Route

路线

Route::put('user/{name}', 'UserController@show');

Controller

控制器

public function update( $name)
{
    User::where('name', $name)->first();
    return view('test')->with('user', $user);
}

Check the Laravel Docs Requests.

检查 Laravel 文档请求

回答by Fellipe Sanches

For those who need to pass part of a url as a parameter (tested in laravel 6.x, maybe it works on laravel 5.x):

对于那些需要将 url 的一部分作为参数传递的人(在 laravel 6.x 中测试,也许它适用于 laravel 5.x):

Route

路线

Route::get('foo/{bar}', 'FooController@getFoo')->where('bar', '(.*)');

Controller:

控制器:

class FooController extends Controller
{
    public function getFoo($url){
    return $url;
    }
}

Test 1:

测试 1:

localhost/api/foo/path1/path2/file.gifwill send to controller and return:

localhost/api/foo/path1/path2/file.gif将发送到控制器并返回:

path1/path2/file.gif

Test 2:

测试 2:

localhost/api/foo/path1/path2/path3/file.docwill send to controller and return:

localhost/api/foo/path1/path2/path3/file.doc将发送到控制器并返回:

path1/path2/path3/file.doc 

and so on...

等等...