Manjunath Singh
Manjunath Singh

Reputation: 95

Laravel - Change base URL globally?

Let assume, the base URL of my application is - http://www.example.com (I have not set anything in any config file to specify this). There are a lot of hard coded urls in the application.

eg. <a href="/contact">Contact</a>

Now, if I go though the application using an URL like - http://www.example.com/country, is it possible to assign a global base URL, where when I click on contact it will take me to - http://www.example.com/country/contact.

There are a lot of such hard-coded URL, changing it individually will take a lot of time (like appending it with a global variable). Is there any simpler way to do this or is there any config specific for this in laravel? I am fairly new to laravel. Any help would be appreciated.

Upvotes: 0

Views: 5007

Answers (2)

Matanya
Matanya

Reputation: 6346

You can use the somewhat cumbersome solution of applying a filter, as suggested by @worldask, but I think it would be better to set a named route and change all occurrences using a regular expression (any decent editor allows for that). That way, for the lifetime of your application you only need to change the routes in routes.php, and it will be reflected everywhere.

e.g

Route::get('country/contact', ['as'   => 'contact', 
                               'uses' => 'SomeController@someFn'];

 <a href="{{route('contact')}}">Contact</a>

Of course, the same principle applies to adding a prefixed group of routes, so you can wrap the entire routes file with a group prefixed by 'country'.

Upvotes: 1

worldask
worldask

Reputation: 1837

you can try Route Prefixing

Route::group(array('prefix' => 'country'), function(){
  Route::get('Contact', 'HomeController@index');
  Route::get('another', 'HomeController@index');
});

edit try route filter

Route::filter('filtername', function($route, $request, $value)
{
    if ($route == 'country') {
      return Redirect::to(url);
    }
});

Upvotes: 0

Related Questions