URL GENERATION
Current URL:
- To retrieve and display the current URL, you can do the following:
- Open an existing Blade view file or create a new one (e.g., resources/views/current-url.blade.php).
- In your Blade view file, add the following code to display the current URL:
In current-url.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Current URL Example</title>
</head>
<body>
<p>Current URL: {{ url()->current() }}</p>
</body>
</html>
In web.php and in UserController
-
- In web.php
// Route to display the current URL example
Route::get(‘/current-url’, [UserController::class, ‘currentUrl’]);
-
- UserController function
public function currentUrl()
{
return view(‘current-url’);
}
Browser Output
Current URL: http://localhost:8000/current-url
Generating Framework URLs:
-
- To generate framework URLs for named routes and controller actions, follow these steps:
- Open an existing Blade view file or create a new one (e.g., resources/views/framework-urls.blade.php).
- In your Blade view file, you can generate URLs using the route() and action() functions. For example:
<!DOCTYPE html>
<html>
<head>
<title>Framework URLs Example</title>
</head>
<body>
@php
use App\Http\Controllers\UserController; @endphp
<p>Profile URL: <a href=”{{ route(‘profile’) }}”>Profile</a></p>
<p>User Edit URL: <a href=”{{ action([UserController::class, ‘showProfile’]) }}”>My Profile</a></p>
</body>
</html>
Asset URLs:
To generate URLs for asset files (CSS, JavaScript, images), follow these steps:
<!DOCTYPE html>
<html>
<head>
<title>Asset URLs Example</title>
<link rel=”stylesheet” href=”{{ asset(‘css/style.css’) }}”>
</head>
<body>
<!– Your HTML content here –>
</body>
</html>
Generation Shortcuts:
Laravel provides shortcuts for generating URLs directly in Blade templates. You can use these
shortcuts within your Blade views. For example:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Shortcuts Example</title>
- </head>
- <body>
- <p>About Us: <a href=”{{ url(‘/admin/dashboard’) }}”>About Us</a></p>
- <p>Profile: <a href=”{{ route(‘profile’) }}”>Profile</a></p>
- </body>
- </html>
[pdf_note link=”https://drive.google.com/file/d/1INmhcXXIq6JciskIR-Xg7cs5Jad3p6Ri/view”]