Mostafa Solati
Mostafa Solati

Reputation: 1235

Laravel 4 optional route parameters

How to achive this route :

/test /test/page-2

I want to pass "2" as parameter and if we are in page 1 just show /test not /test/page

Upvotes: 0

Views: 54

Answers (1)

c-griffin
c-griffin

Reputation: 3026

I agree with the above comment, why not just use the page number alone as a parameter?

Routes:

Route::get('test', ['uses' => 'TestController@index']);    
Route::get('test/page/{pageNumber?}', ['uses' => 'TestController@index']);

TestController method:
Assuming your views are in a directory tests and are named testPage-1, testPage-2, testPage-3, etc...

public function index($pageNumber = 1){
    return View::make('tests.testPage-'.$pageNumber);
}

Upvotes: 1

Related Questions