How To Create URLs For Named Routes In Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Named routes allow you to generate URLs without being coupled to the actual URL defined on the route.

How To Create URLs For Named Routes In Laravel Framework

URLs For Named Routes

The route helper may be used to generate URLs to named routes. Named routes allow you to generate URLs without being coupled to the actual URL defined on the route. Therefore, if the route's URL changes, no changes need to be made to your route function calls. For example, imagine your application contains a route defined like the following:
 

Route::get('/post/{post}', function () {
//
})->name('post.show');
 

To generate a URL to this route, you may use the route helper like so:

echo route('post.show', ['post' => 1]);

// http://example.com/post/1



You will often be generating URLs using the primary key of Eloquent models. For this reason, you may pass Eloquent models as parameter values. The route helper will automatically extract the model's primary key:

echo route('post.show', ['post' => $post]);



The route helper may also be used to generate URLs for routes with multiple parameters:

Route::get('/post/{post}/comment/{comment}', function () {
//
})->name('comment.show');

echo route('comment.show', ['post' => 1, 'comment' => 3]);

// http://example.com/post/1/comment/3



Signed URLs
 

Laravel allows you to easily create "signed" URLs to named routes. These URLs have a "signature" hash appended to the query string which allows Laravel to verify that the URL has not been modified since it was created. Signed URLs are especially useful for routes that are publicly accessible yet need a layer of protection against URL manipulation.


For example, you might use signed URLs to implement a public "unsubscribe" link that is emailed to your customers. To create a signed URL to a named route, use the signedRoute method of the URL facade:
 

use Illuminate\Support\Facades\URL;

return URL::signedRoute('unsubscribe', ['user' => 1]);


If you would like to generate a temporary signed route URL that expires, you may use the temporarySignedRoute method:
 

use Illuminate\Support\Facades\URL;

return URL::temporarySignedRoute(
'unsubscribe', now()->addMinutes(30), ['user' => 1]
);



Validating Signed Route Requests


To verify that an incoming request has a valid signature, you should call the hasValidSignature method on the incoming Request:
 

use Illuminate\Http\Request;

Route::get('/unsubscribe/{user}', function (Request $request) {
if (! $request->hasValidSignature()) {
abort(401);
}

// ...
})->name('unsubscribe');



Alternatively, you may assign the Illuminate\Routing\Middleware\ValidateSignature middleware to the route. If it is not already present, you should assign this middleware a key in your HTTP kernel's routeMiddleware array:
 

/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];



Once you have registered the middleware in your kernel, you may attach it to a route. If the incoming request does not have a valid signature, the middleware will automatically return a 403 error response:
 

Route::post('/unsubscribe/{user}', function (Request $request) {
// ...
})->name('unsubscribe')->middleware('signed');


Example(1)

1.Create Route in web.php file as below created

Route::get('employee/details/example',array
('as'=>'employee.details',function()
    {
        $url=route('employee.details');
        return "The url is : " .$url;
    }));


2.Run below url to see output of above route.

http://localhost/laraveldemoproject/public/employee/details/example

 

Related Post