2016-12-17 6 views
1

私の見解は、このようなものです:web.phpが似ている\laravel 5.3でパラメータを取得するには?

localhost/mysystem/public/users?2016

マイルート:私は2016年に、リンク上にカーソルを移動すると、URLは次のようになります

<li class="{{ Request::is('users*') ? 'active' : '' }}"> 
    <a href="{!! route('users.index', 2016) !!}"> 
     <i class="fa fa-circle-o"></i> YEAR 2016 
    </a> 
</li> 
<li> 
    <a href="{!! route('users.index', 2017) !!}"> 
     <i class="fa fa-circle-o"></i> YEAR 2017 
    </a> 
</li> 

この:

Route::get('users/index?{year}', 'UserController'); 

Route::resource('users', 'UserController'); 

そして、私のコントローラのユーザーは、このようなものです:

あり
public function index(Request $request) 
{ 
    $year = $request->input('year'); 
    echo $year;die() 
} 

は、このようなエラーが存在します。

UnexpectedValueException in Route.php line 646: Invalid route action: [App\Http\Controllers\UserController] 

私を助けることができるすべての人々がありますか?

答えて

1

あなたのルートはのようにする必要があります:

Route::get('users/index/{year}', '[email protected]')->name('users.index.year'); 

あなたのコントローラとして:あなたがからそれを取得します

{{ route('users.index.year', ['year' => 2016]) }} 
+0

このようなエラーがあります: 'UserControllerのErrorException。php行36:App \ Http \ Controllers \ UserController :: index()の引数2がありません –

+0

同じです。それは動作しませんでした –

+0

あなたのリソース行 'Route :: resource( 'users'、 'UserController');'をコメントアウトして、もう一度試してみてください。 –

0

public function index(Request $request, $year) 
{ 
    echo $year;die() 
} 

は、次に、あなたとあなたのビューでそれを使用することができますメソッドの引数

public function index(Request $request,$para_year) 
{ 
    $year = para_year; 
    echo $year;die() 
} 

、あなたがルートは、このようなルートで

Route::get('users/index/{year}', '[email protected]'); 
1

パスパラメータでなければなりません:

Route::get('users/index/{year}', '[email protected]'); 

ビュー:

<a href="{!! route('users.index', ['year' => 2016]) !!}"> 
    <i class="fa fa-circle-o"></i> YEAR 2016 
</a> 

コントローラ(コントローラのメソッド内の引数として取得):

public function index($year) 
{ 
    echo $year; 
    die(); 
} 

か - あなたがルートにGETのparamsは、ちょうどこのそうであるようにパラメータを渡す場合:

Route::get('users/index', 'UserController'); 

とあなたの<a href="">内側:

<a href="{!! route('users.index', ['year' => 2016]) !!}"> 
    <i class="fa fa-circle-o"></i> YEAR 2016 
</a> 

声明:{!! route('users.index', ['year' => 2016]) !!}をルートを作成します。 http://website.com/users/index?year=2016

を使用してコントローラで取得しますこのようなヘルパー:

public function index() 
{ 
    $year = request()->get('year'); 
    echo $year; 
    die(); 
} 

これは役に立ちます。

+0

私はあなたの助けが必要です。これを見てください:http://stackoverflow.com/questions/41788737/why-the-deleted-data-still-appear-in-the-interface –

関連する問題