user3263978
user3263978

Reputation: 193

Pretty URL using laravel 4.2

I'm creating a website using Laravel (first time user) and I have managed to have URL's such as

localhost/branch/pass

so without using the .php at the end of pass. But what I'm trying to achieve is this, when a user visits the 'pass' page they should take a token with them which is sent via email so they should visit it like this...

localhost/branch/pass?token=blah

what I want the URL to look like is...

localhost/branch/pass/blah

I've achieved this before in .htaccess while NOT using laravel I was just wondering if laravel had their own way of doing this or will it be the same as I've previously done?

Thanks in advance!

Upvotes: 0

Views: 487

Answers (2)

user1669496
user1669496

Reputation: 33148

There shouldn't be any need to modify your .htaccess files to do this with Laravel as it will all be handled internally.

Create your route.

Route::get('branch/pass/{token}', array('uses' => 'YourController@yourFunction', 'as' => 'passwordRoute'));

One thing to note here is I named the route to passwordRoute. You can change the name to whatever you want. I find this very useful because in the future if you need to make changes to the route, as long as the name stays the same, you will always be able to find it by its name.

Create your function which you probably already have somewhere.

class YourController extends BaseClass {

    public function yourFunction($token)
    {
        // Do stuff with your token.
    }
}

Now you can create urls to this route very easily by using URL::route() and passing in the route's name.

URL::route('passwordRoute', array('token' => $token));

Here, we are calling the route by it's name we already defined. This will grab the route, see that the route needs a parameter for a token, and then replace that token parameter with whatever is stored in your $token variable. Of course, your variable might be different you may need to change $token to whatever variable you are using to store the actual token.

In your case, it would be whatever variable is set to blah.

This function will then output the URL which you are looking for based on the route and the parameter.

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50798

In your Routes file, we can filter for dynamic properties using {} in the path. Being that this is a GET request, we can just use Route::get.

Route::get('branch/pass/{token}', 'MyController@MyFunction');

Then you can properly make your URL's in the email like:

http://localhost/branch/pass/blah

and the blah which is your token, will be passed as an argument to the function you declared within your controller.

Upvotes: 1

Related Questions