Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Developer654079525's avatar

Best practice for dealing with links

What's good practice for dealing with links in blade templates during development vs deployment? Having relative paths, storing the site into a var or something else?

<a href="/relative/...">...
<a href="{{ $some_global_site_var }}/relative_path">...

ultimately, we would like to have the full links in the deployed site:

<a href="example.com/the_rest">...
0 likes
8 replies
Tray2's avatar
Tray2
Best Answer
Level 73

In my opinion you should use named routes whenever possible.

//web.php
Route::get('/records', [RecordController::class, 'index'])->name('records.index');
Route::get('/records/{id}', [RecordController::class, 'show'])->name('records.show');
//Blade file
<a href="{{ route(records.index }}">Records</a>

oh, and by the way, it will give you the relative path, there is no need to use the full path.

1 like
Developer654079525's avatar

@Tray2 I see, many thanks. So a named route like ->name('records.index'); does not resolve to example.com? It simply stays as a relative one?

Snapey's avatar

best practice is to not hardcode urls in your views. Use a method that resolves the url at runtime such as the route or URL helpers

1 like
Developer654079525's avatar

@Snapey I see. If I explicitly wanted to fetch a base url that represents a live site like example.com which helper fn would you recommend in production?

Snapey's avatar

@Developer654079525 you can get either from the route helper, by specifying true or false as the third parameter.

But really, it should not matter. Relative urls work perfectly well.

I can't check at the moment, but I'm pretty sure, absolute urls sre the default?

Snapey's avatar

Yes @snapey you were right. The route helper generates Absolute, full URI links unless you go out of your way to override it.

martinbean's avatar

What's good practice for dealing with links in blade templates during development vs deployment?

@developer654079525 By not hard-coding them in the first place.

Use the actual functionality given to you by Laravel such as the route helper, which will generate proper URLs regardless of what your application’s base URL is:

<a href="{{ route('home') }}">Home</a>

You’re asking lots of basic questions that it’s clear you need to spend a little time reading the Laravel docs instead of just writing code, not knowing about fundamental things like this, and teaching yourself lots of bad practices and/or inefficiencies.

A few hours reading the Laravel docs will save you weeks of un-doing bad habits you’ll have picked up by ignoring them in the first place.

Please or to participate in this conversation.