Generating url relative to the base url in laravel 4

23,639

Solution 1

To generate a relative URL, you can use URL::route or URL::action as they allow to pass a $absolute parameter which defaults to true. So to get a relative URL when using named routes for example, you can use the following:

URL::route('foobar', array(), false)

This will generate a URL like /foobar.

Solution 2

First you need to create a Named Route like

Say yo want to go to http://baseurl/user and runs the method 'showuser' define in controller 'allusers'

then your Route shold look like this:-

Route::get('user', array('as' => 'myuser', 'uses' => 'allusers@showuser'));

Now your URL to /user would be

$myuserurl = URL::to('/myuser');
echo $myuserurl; // would be http://baseurl/user

I hope this helps you. Pls refer http://laravel.com/docs/routing#named-routes

Share:
23,639
Suresh
Author by

Suresh

Full-Stack Software Engineer having 10Yrs of industrial experience in developing large scale web/mobile applications. Indian by born, currently staying in UAE. Nowadays, offering my expertise from my couch. I can help you in building your website create your MVP or web/mobile application, if you have any product idea design a custom solution to automate your business process On off days I love to travel, meet new people & capture the beauty of this world.

Updated on October 07, 2020

Comments

  • Suresh
    Suresh over 3 years

    I'm new to Laravel & right now building one application on L-4 but got stuck at one place. Can't able to understand how to generate url relative to base url. In laravel-3 i know this can be done by

    $url = URL::to('user/profile'); 
    

    But, in L-4 how we can do this.. ?

  • user1669496
    user1669496 almost 11 years
    Shouldn't that be URL::to('myuser'); since it would be calling the route's name to figure out where to link to rather than linking directly to the route? If it's URL::to('/myuser'), I believe the route should be Route::get('/myuser', array('as' => 'myuser', 'uses' => 'allusers@showuser'));
  • deepika jain
    deepika jain almost 11 years
    You can define Route without a leading slash (/) and can use directly URL:: Route to get the named Route URL as:- Route::get('myuser', array('as' => 'myuser', 'uses' => 'allusers@showuser')); Usage -- URL::route('myuser', array())
  • sebt
    sebt over 9 years
    This is the closest to being a correct answer to the question. URL::route() generates a URL to a named route, and the last false parameter stipulates that the generated route is relative. For clarity, the generated URL will be that which the named route defined and not necessarily /foobar
  • sebt
    sebt over 9 years
    Downvoted as the answer is misleading. URL::to() won't route to a named route, nor will it generate a relative URL.
  • Admin
    Admin over 9 years
    @sebt I understand your wish to help, but unfortunately we can't accept your edit because that code clearly conflicts with that the author said. Why not post your improved solution as your own answer instead ?
  • sebt
    sebt over 9 years
    @AndréDaniel Understood. After submitting the edit in somewhat of a rush, I realised that it conflicts with SO policy on edits. Couldn't figure out how to retract it, sorry. In the end I added a comment to Holger Weis' answer which indicates the correct approach fairly clearly.