Basic routing in Laravel

Last update: 05-05-2024

For basic routing, updating your web.php file might be sufficient.

Example:

<?php

use IlluminateSupportFacadesRoute;

Route::get('/', function () {
    return'
    <h1>Home page</h1>
    <a href="/plain-text">Plain text</a>
    <a href="/html-page">Rendered HTML</a>
    <a href="/json-example">json</a>
    ';
});
Route::get('/plain-text', function () {
    return response('
    <h1>Plain text</h1><p>The HTML tags are not rendered</p>
    ')->header('Content-type', 'text/plain');
});
Route::get('/html-page', function () {
    return '
    <h1>Rendered HTML</h1>
    <ul>
        <li>First item</li>
        <li>Second item</li>
    </ul>
    ';
});
Route::get('/json-example', function () {
    return response('
    {
        "key1":
        {
            "sub-key1": "sub-value1",
            "sub-key2": "sub-value2",
        }
        "key2":
        {
            "sub-key3": "sub-value3",
            "sub-key4": "sub-value4",
        }
    }    
    ')->header('Content-type', 'application/json');
});

0 Comments

Add a new comment: