how to access URLs using a function in Laravel

How to access URLs using a function in Laravel
How to access URLs using a function in Laravel

Hi guys,

Today we are learning how to access URLs using a function in Laravel. Laravel allows you to access URLs like the current, full, and previous methods. In this article, We are dealing with Reverse Route as well as localization. So let’s begin.

The Basics:-

The URL helper may generate arbitrary URLs for your application. The generated URL will automatically use scheme (HTTP or HTTPS) and host from the current request:

 $post = App\Post::find(1);

 echo url("/posts/{$post->id}");

Accessing The URL:-

Laravel gives different function to access URL in your application as below:

 // Get the current URL without the query string…
 echo url()->current();

 // Get the current URL including the query string…
 echo url()->full();

 // Get the full URL for the previous request…
 echo url()->previous();

URLs For Named Routes:-

Named routes allow you to make URLs without combining them with the actual URL defined on the route.

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

 // http://devrohit.com/

You can also pass multiple parameter to the associative array as below:

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

 // http://devrohit.com/

Signed URL:-

Laravel allows you to verify URL that URL has not changed yet since they created it using Singed URL Feature. A signed URL is helpful when routes are publicly accessible. It added an extra layer to the URL. The best Example, Unsubscribe link that emails to your customers.

 use Illuminate\Support\Facades\URL;

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

If you want to create a temporary signed route URL that expires using temporarySignedRoute method:

 use Illuminate\Support\Facades\URL;

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

Also, you want to check whether the request URL has a valid signature using hasValidSignature method from the Request Package.

 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 the request URL does not have a valid signature, the Middleware will automatically redirect the return 403 error response.

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

URLs for controller actions:-

The action function generates a URL for the controller action.

 $url = action('HomeController@index');

If you want to pass route parameters to controller method use given below function:

 $url = action('UserController@profile', ['id' => 1]);

Default Values:-

You want to specify request-wide default values for certain URL parameters. You may use the URL::defaults method to define a default value for this parameter that will always apply to the current URL. So you can do this on Middleware Like below:

 <?php

 namespace App\Http\Middleware;

 use Closure;
 use Illuminate\Support\Facades\URL;

 class SetDefaultLocaleForUrls
 {
     public function handle($request, Closure $next)
     {
         URL::defaults(['locale' => $request->user()->locale]);
         return $next($request);
     }
 }

You do not need to pass a parameter when generating a URL If you set the locale parameter.

I am pretty sure this article (how to access URLs using a function in Laravel) it help you a better understanding of the URLs function in Laravel. I have followed this link for this article. If you have any doubt, then comment below then I will reply as per as possible.

Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers,

Leave a Comment

Your email address will not be published. Required fields are marked *