URL Generation β
Doppar provides several helpers to assist you in generating URLs for your application. These helpers are primarily helpful when building links in your templates, or when generating redirect responses to another part of your application. The most basic URL generation will be like assume we want to generate url using route name.
Generating URLs β
Doppar provides a convenient url helper function that can be used to generate full URLs within your application. This helper automatically uses the scheme (HTTP or HTTPS) and host from the current request context.
use App\Models\Post;
$post = Post::find(1);
echo url("/posts/{$post->id}");
// http://example.com/posts/1
You can use to()
function to generate url like
use Phaseolies\Support\Facades\URL;
URL::to('products')->make(); // using URL facades
url()->to('products')->make(); // using helper function
// http://example.com/products
To generate a URL with query string parameters, you may use the query method:
return url()->to('products', ['category' => 'electronics', 'page' => 2])->make();
// http://example.com/products?category=electronics&page=2
Secure URL β
To generate a secure (HTTPS) URL, Doppar provides support through the UrlGenerator class. By enabling the secure flag, all generated URLs will use the https://
scheme:
url()->to('checkout')->make();
// https://example.com/checkout
With Fragment β
You can append a fragment (hash) to the end of a URL using withFragment()
:
url()->to('about')->withFragment('team')->make();
// http://localhost:8000/about#team
Signed URL β
Generate a temporary, signed URL (useful for secure actions like downloads or password resets):
url()->signed('download', ['file' => 'report.pdf'], 3600);
// http://example.com/download?file=report.pdf&expires=...&signature=...
You can also create this above URL using
url()->to('/download')
->withQuery(['file' => 'report.pdf'])
->withSignature(3600)
->withFragment('about')
->make();
// output
http://localhost:8000/download?file=report.pdf&expires=1742401435&signature=363ef0e47fd9fca7197882490ee8f4c132df6b9b6e9e0041ac0df5c31cc349d3#about
Verify Signed URL β
To ensure that your application only processes requests with valid signed URLs, you can use the hasValidSignature()
method. This method checks whether the URL has been properly signed and hasnβt been tampered with or expired. Hereβs an example of how to use it in your code:
if ($request->hasValidSignature()) {
// Valid signed url
}
This ensures that only requests with authentic signatures are allowed to execute sensitive actions, improving the security of your application.
Route Generation β
Use named routes to generate URLs dynamically:
url()->route('user.profile', ['id' => 123]);
// http://example.com/users/123/profile
Current URL β
Retrieve the full URL of the current request:
url()->current();
// http://example.com/current-path
Full URL with Query β
Get the full current URL including query parameters:
url()->full();
// http://example.com/current-path?with=query
Changing Security Setting β
Toggle between HTTP and HTTPS programmatically:
url()->to('login')->setSecure(true)->make();
// Output: https://example.com/login
URL Validation β
Check if a given string is a valid URL:
url()->isValid('https://valid.com'); // true
url()->isValid('invalid url'); // false
Accessing public assets β
Doppar provides enqueue() method to access your public assstes like
url()->enqueue('/assets/example.png');
// http://example.com/assets/example.png
Get Base URL β
To get base URL, you can call base()
method like
url()->base();
// http://example.com
URL Facades β
You can generate all the above URL using Phaseolies\Support\Facades\URL
facades. Doppar provides both ways to generate custom URL. Let's see an example of generating URL using URL facades.
use Phaseolies\Support\Facades\URL;
URL::to('/download')
->withQuery(['file' => 'report.pdf'])
->withSignature(3600)
->withFragment('about')
->make();
This fluent API makes it easy to build complex URLs cleanly and consistently.