1. Go to this page and download the library: Download roy404/routes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
roy404 / routes example snippets
Route::group(['middleware' => 'auth'], function () {
Route::get('/dashboard', function () {
echo 'Welcome to the Dashboard';
});
Route::get('/profile', function () {
echo 'Your Profile';
});
});
Route::controller(HomeController::class)->group(function() {
Route::get('/home', 'index');
});
/**
* Explanation:
*
* Controller Registration: The Route::controller(HomeController::class) method registers the HomeController
* to handle specific routes within the group. This means that any route defined within the group will be
* handled by the controller's methods.
*
* Defining Routes: Inside the group, the Route::get('/home', 'index') defines the `/home` route, which
* will be handled by the `index` method of the HomeController.
*/
Route::prefix('admin')->group(function () {
Route::get('/dashboard', function () {
echo 'Admin Dashboard';
});
});
/**
* Explanation:
*
* Prefixing Routes: The Route::prefix('admin') method adds the 'admin' prefix to all the routes inside the group.
* In this case, /dashboard will be accessible at /admin/dashboard.
*
* Defining Routes: Inside the group, we define the /dashboard route, which will display the message 'Admin Dashboard'.
*/
Route::name('user')->group(function() {
Route::get('home', function() {
echo 'Your home';
})->name('home');
Route::get('profile', function() {
echo 'Your profile';
})->name('profile');
});
/**
* Once your routes are set up, you can easily retrieve their URLs by calling the route name.
* This is especially useful when you need to generate links dynamically.
*
* Result:
* "user.home" => '/home'
* "user.profile" => '/profile'
*/
Route::configure(__DIR__, [
'routes/web.php' // Add all route files here, you can add more as needed.
])->routes(function (array $routes) {
/**
* Retrieve all the registered routes here,
* you will be able to see all the details of each routes registered.
*/
})->captured(function (mixed $content, int $code, string $type) {
// Handle the response here
http_response_code($code);
header('Content-Type: ' . $type); // Set the content type (e.g., 'text/html', 'application/json').
echo $content; // Output the response content.
});
use App\Routes\Route;
Route::get('/', function () {
echo 'Hello World!';
});