Home > database >  simulate route in laravel
simulate route in laravel

Time:10-12

I have this route defined

Route::get('test/something/{id?}', 'MyController@mymethod')->name('test.something');

So if go to domain_1.com/test/something/123 I get some page with data.

Now, I want to to show the exact same thing if the site if accessed form another domain with subdomain. I've defined this:

Route::group(['domain' => 'subdomain.domain_2.com'], function() {
    Route::get('/', 'MyController@mymethod', ['id' => 123]);
});

but I got to subdomain.domain_2.com, I get Too few arguments error. How can I pass the id parameter to the method in the route?

CodePudding user response:

You could use redirect like:

Route::get('test/something/{id?}', 'MyController@mymethod')->name('test.something');

Route::group(['domain' => 'subdomain.domain_2.com'], function() {
    
    Route::get('/', function(){
        return redirect('/test/something/123');
    });

});

If you don't want to use redirect, you could check the current url from the method in the controller like:

public function mymethod($id=null){
    $currentUrl = request()->url();

    if (strpos($currentUrl, 'subdomain') !== false) {
        $id = 123;
    }

    // your function
}

CodePudding user response:

I've found this piece of code that gets the job done, but I'm not sure is the correct way to achieve what I want

Route::group(['domain' => 'subdomain.domain_2.com'], function()
{
    Route::get('/', [ 'as' => 'some_alias', function()
    {
        return app()->make(App\Http\Controllers\MyController::class)->callAction('mymethod', $parameters = [ 'id' => 123 ]);
    }]);    
});
  • Related