4

I have a route like that:

Route::get('category/{id}/{date?}', array('as' => 'category/date', 'uses' => 'Controller@getCategory'));

I want to run @getCategory with default parameters when called '/' root route. So if '/' route called, getCategory function should run with id=1 and date=2015-12-18.

How should I do that?

horse
  • 707
  • 4
  • 11
  • 30

5 Answers5

13

Register it as a separate route:

Route::get('/', 'Controller@getCategory')->named('home');
Route::get('category/{id}/{date?}', 'Controller@getCategory')->named('category/date');

Then in your controller, set default values for those arguments:

public function getCategory($id = 1, $date = '2015-12-18')
{
    // do your magic...
}
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
3

Set a default value for the route with Laravel 9.x


Route::get('categories/{id}/{date?}', 'CategoryController@getCategory')
     ->name('category-date')
     ->defaults('date', '2023-02-13');

Suhail Kawsara
  • 466
  • 3
  • 11
2

first, please excuse me for my bad English writing. to answer the question of emotality and Robert : we can use a class in routes/web.php and then in controller use it, for example :

Routes/web.php

class SomeVars {
    public $var1 = 'en';
    public $var2 = 'fr';
}   
Route::get('/localization/{vars}','LocalizationController@index');

LocalizationController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use SomeVars;
class LocalizationController extends Controller {
    public function index(Request $request, SomeVars $vars) {
        echo $vars->var1;
        echo $vars->var2;
    }
}   

Now if you browse : someSite/localization/anything see this result :

en fr


and if you need this class in the all method of your controller , you can use this way :

Routes/web.php

class SomeVars {
    public $var1 = 'en';
    public $var2 = 'fr';
}   
Route::get('/localization','LocalizationController@index');

LocalizationController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use SomeVars;
class LocalizationController extends Controller {
    public $vars;
    public function __construct(SomeVars $vars){
        $this->vars = $vars;
        
    }
    public function index(Request $request) {
        echo $this->vars->var1;
        echo "<br>";
        echo $this->vars->var2;
    }
}           

Now, if you can this address in your browser : someSite/localization can see this result again :

en fr

i hope this help, thanks, hope to God

0

The routes, For example in My case I had polymorphic relationship for comments

 Route::get('posts-comments/{commentable_id}', ['uses' => 'CommentController@fetchComments', 'commentable' => 'posts']);

 Route::get('video-comments/{commentable_id}', ['uses' => 'CommentController@fetchComments', 'commentable' => 'videos']);

Then In the controller:

 public function fetchComments(Request $request, commentable_id)
  {
    $commentable = $request->route()->getAction()['commentable'];
  }

I hope this answers your question

James Ikubi
  • 2,552
  • 25
  • 18
-1

It works for me with a "?" {date?} in the route, and putting a default value in the anonimus function.$date = null

Route

Route::get('category/{id}/{date?}', function($date = null) {
   if ($date === null)
      //Option 1
   else
      //Option 2    
});
Unai Susperregi
  • 398
  • 3
  • 16