Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating url relative to the base url in laravel 4

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.. ?

like image 924
Suresh Avatar asked May 28 '13 10:05

Suresh


People also ask

How set API base URL in Laravel?

You can use that code but replace the line $generator->forceSchema('https'); in the provider boot method, with $generator->forceRootUrl(Request::getScheme() . '://www.yourdomain.com'); . And now your URL should be generated like you want.

What is @param in Laravel?

The required parameters are the parameters that we pass in the URL. Sometimes you want to capture some segments of the URI then this can be done by passing the parameters to the URL. For example, you want to capture the user id from the URL.


2 Answers

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.

like image 120
Holger Weis Avatar answered Oct 18 '22 02:10

Holger Weis


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

like image 45
deepika jain Avatar answered Oct 18 '22 00:10

deepika jain