laravel 在路由中发送参数

laravel send parameter in route

无论如何他们都会做这样的事情,

在web.php

Route::get('/test', 'testController@test');

正在测试控制器

public function test ($url)
{
 //while $url store test in route 
}

我知道只有当我发送参数时我才必须使用

Route::get('/{test}', 'testController@test');

更新

我想做这样的事情

Route::get('/test', 'testController@test');
Route::get('/test2', 'testController@test'); 

在我的控制器中

 public function test ($url)
   {
     while $url store test,test2in route 
   }

最近更新

我不想使用 {url}

我想在输入 url/test

时创建 /test = $url

在我的 web.php 我用这个

Route::get('/test', 'testController@test');
Route::get('/test2', 'testController@test'); 

我想做这样的事情的原因是因为我想制作 alll 路由可以使用的 1 个函数在我的控制器中我这样做了。

 public function test($url,$preview=null)
    {   
    //$url shoud be test or test 2
        try {
            $test = (isset($preview)) ?  test::where('test.id',$url)->first()

        } catch (\Exception $e) {
            return redirect('notfound');
        }   
    }

我不想做这样的事情

Route::get('/test', 'testController@test');
Route::get('/test2', 'testController@test'); 

并在控制器中

public function test($preview=null)
    {   
    //$url shoud be test or test 2
        try {
            $test = (isset($preview)) ?  test::where('test.id','test)->first()

        } catch (\Exception $e) {
            return redirect('notfound');
        }   
    }

你需要结合这两个元素

Route::get('/test/{url}', 'testController@test');

want to make /test = $url

你不能,但你可以 /test?foo=$url 代替。所以你保持你的路线像

Route::get('/test', 'testController@test');

然后添加 Request $request 作为控制器方法参数(然后删除 $url

public function test(Request $request) {
   ... 

你终于用

获得了你的url
$url = $request->input('foo');

你的路线

Route::post('/test', 'testController@test')->name('test);

如果你使用blade.

<a href="{{ route('test') }}" 
         onclick="event.preventDefault(); 
         document.getElementById('test_id').submit();">
         Test Click
</a>

{!! Form::open(['url' => route('test'), 'method' => 'post', 'id' => 'test_id']) !!} 
     <input type="hidden" name="url" value="{{ $url}}">

{!! Form::close() !!}

在你的控制器中。

public function test(Request $request)
{   
    $data = $request->all();
    $url = $data['url'];
    //do something with your url...  
}