Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Jakarta EE 11 Platform launches with modernized Test Compatibility Kit framework

      June 26, 2025

      Can Good UX Protect Older Users From Digital Scams?

      June 25, 2025

      Warp 2.0 evolves terminal experience into an Agentic Development Environment

      June 25, 2025

      Qodo launches CLI agent framework

      June 25, 2025

      My laptop webcam wasn’t cutting it for video calls – then I discovered this accessory

      June 26, 2025

      The top 6 TVs ZDNET readers are buying (no. 1 has the best picture quality we’ve ever seen)

      June 26, 2025

      You should probably delete any sensitive screenshots you have in your phone right now. Here’s why

      June 26, 2025

      Can these $100 Android phones replace my flagship? The result after weeks of testing

      June 26, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      A bot posting the Echo JS RSS feed to Bluesky

      June 26, 2025
      Recent

      A bot posting the Echo JS RSS feed to Bluesky

      June 26, 2025

      Accepting Multiple Parameters in Laravel Commands

      June 26, 2025

      Translate Your App to Other Languages With Laravel Gemini Translator

      June 26, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Distribution Release: deepin 25

      June 26, 2025
      Recent

      Distribution Release: deepin 25

      June 26, 2025

      SpicyPass is a lightweight password manager

      June 26, 2025

      Raspberry Pi 5 Desktop Mini PC: 2.5Gbps Networking

      June 26, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Laravel Routing

    Laravel Routing

    May 10, 2025

    Routes are your Laravel Application’s entry point, defining how it responds to different URLS and HTTP requests.

    A route is a way to specify the URL patterns your application should respond to what action to take when a specific URL is accessed. Routes are defined in the routes folder, particularly in web.php (Web routes) and api.php (API routes). Each route has a URL path and an associated callback function or controller method that handles the request.

    Basic Routing

    1. Open the routes/web.php file.
    2. Define a route using Route::get to specify that this route should respond to GET Requests.

    Example: Basic route that returns text.

    Route::get('/hello', function(){
         return "Welcome, to PythonCodeVerse"
    });

    Explanation:

    Route::get: This specifies that this route only responds to GET Requests.

    '/hello': This is the URL path. When you visit HTTP://localhost:800/hello, this route will be triggered.

    Callback function: The anonymous function (function () {……}) returns ‘Welcome, to PythonCodeVerse’ to user.

    To see the output, start your Laravel server with:

    php artisan serve

    Open your browser and go to http://localhost:8000/hello. You should see “Welcome, to PythonCodeVerse” displayed on the screen.

    HTTP Requests: GET, POST, PUT, DELETE

    HTTP requests allow clients (Like browsers) to communicate with the server, each with its specific purpose:

    GET: Retrieves data from the server, typically used for displaying pages.

    POST: Sends data to the server, commonly used for submitting forms

    PUT: Update existing data on the server.

    DELETE: Removes data from the server.

    Defining GET and POST Routes

    In Laravel, you can define routes for different HTTP request types by replacing Route::get with Route:post, Route:put, or Route::delete.

    Example: POST Route

    Route::post('/submit', function(){
    return 'Form Submitted'
    })

    In this example, the /submit route will respond to POST requests. As shown below, you would typically use a form to make this request.

    <form action="/submit" method="POST">
    @csrf
    <button type="submit">Submit</button>
    </form>

    Here, we set method=”POST” to make a POST request to the /submit route.

    Notice the @csrf directive, which adds a CSRF (Cross-Site Request Forgery) token for security. Laravel requires this token for all POST, PUT, and DELETE requests to prevent unauthorised access.

    PUT and DELETE Routes

    To handle updates and deletions, we use PUT and DELETE requests, which are often used in RESTful applications.

    //PUT
    Route::put('/update', function (){
    return 'Data updated!';
    }
    );
    
    //DELTE
    Route::put('/delete', function (){
    return 'Data deleted!';
    }
    );

    Just like post, you would typically make PUT and DELETE requests via form or JavaScript, setting the method accordingly. Laravel makes it easy to specify within a form by using hidden fields.

    Example: PUT request with a form

    <form action="/update" method="POST">
    @csrf
    @method('PUT')
    <button type="submit">Update</button>
    </form>

    In the above example, we use @method('PUT') to indicate that this form should be treated as a PUT request. This tells Laravel to match the form submission to the /update route defined with Route::put

    Dynamic Routes with Route Parameters

    Laravel allows you to create dynamic routes by using parameters in the URL. This is particularly useful for situations where you want to pass specific information, like user id or product id, product name, through the URL.

    Example: Defining a route with a parameter

    Route::get('/user/{id}', function($id){
       return "User id: $id"
    });

    In this example:

    • {id} is a route parameter. Laravel will capture this value from the URL and pass it to the callback function as the $id variable.
    • When you access http://localhost:8000/user/1, Laravel will display “User id:1” because the $id parameter is set to 1.

    Multiple Parameters

    Example: A route with two parameters

    Route::get('/user/{id}/post/{postid}', function($id, $postid){
       return "User id: $id, Post ID: $postid";
    });

    This route will respond to URLS like http://loclahost:8000/user/1/post/5, displaying “User id:1 Post id:5”.

    Optional Parameters

    Sometimes, you may want to make a route parameter optional. To do this, add a ? after the parameter name and provide a default value in the callback function.

    Example: Defining a route with an optional parameter

    Route::get('/user/{name?}', functon
    ($name='Guest'){
    return "Hello, $name!";
    });

    In this example, if you visit http://localhost:8000/user/john, it displays “Hello, john!” if you omit the name

    Ex: http://localhost:8000/user , It displays “Hello, Guest!”

    Named Routes

    Named routes allow you to assign a name to a route, making it easy to reference in your application. This is particularly useful when generating URLS or redirecting to specific routes.

    Example: Defining a named route

    Route::get('/dashboard', function(){
      return view('dashboard');
    })->name('dashboard');

    You can then reference this route by its name elsewhere in your application, like in links or redirects.

    Example: Generating a URL to a named route.

    $url = route('dashboard'); // Generates URL for /dashboard

    Grouping Routes

    Laravel lets you group routes, which is useful for organising routes that share a common feature, like middleware or prefixes.

    Example: Grouping routes with a prefix.

    Route::prefix('admin')->group(function(){
          Route::get('/dashboard', function(){
          return 'Admin Dashboard'
         });
          Route::get('/setting', function(){
          return 'Admin setting'
         });
    });

    With this setup, you can access the routes at http://localhost:8000/admin/dashboard and http://localhost:8000/admin/settings. The prefix method automatically prepends “admin” to each route in the group.

    Route Middleware

    Middleware allows you to filter requests by applying a login before the requests reach their intended destination route. Common use cases include authentication and access control.

    Example: Protecting a route with authentication middleware.

    Route::get('/profile', function (){
     return view('profile');
    })->middleware('auth');

    In this example, the auth middleware ensures that only authenticated users can access the /profile route.

    The post Laravel Routing appeared first on PHPGurukul.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleCannot Delete Emails from Deleted Items Folder in Office 365 Mailbox
    Next Article LWiAI Podcast #208 – Claude Integrations, ChatGPT Sycophancy, Leaderboard Cheats

    Related Posts

    Security

    Critical RCE Flaws in Cisco ISE and ISE-PIC Allow Unauthenticated Attackers to Gain Root Access

    June 26, 2025
    Security

    HPE OneView for VMware vCenter Allows Escalation of Privileges

    June 26, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    A Step-by-Step Coding Guide to Defining Custom Model Context Protocol (MCP) Server and Client Tools with FastMCP and Integrating Them into Google Gemini 2.0’s Function‑Calling Workflow

    Machine Learning

    CISA Warns of iOS 0-Click Vulnerability Exploited in the Wild

    Security

    Critical Security Vulnerability Found in WordPress Plugin InstaWP Connect

    Development

    Simple ReFlow: Improved Techniques for Fast Flow Models

    Machine Learning

    Highlights

    CVE-2025-6300 – PHPGurukul Employee Record Management System SQL Injection Vulnerability

    June 20, 2025

    CVE ID : CVE-2025-6300

    Published : June 20, 2025, 3:15 a.m. | 3 hours, 26 minutes ago

    Description : A vulnerability classified as critical was found in PHPGurukul Employee Record Management System 1.3. This vulnerability affects unknown code of the file /admin/editempeducation.php. The manipulation of the argument yopgra leads to sql injection. The attack can be initiated remotely. The exploit has been disclosed to the public and may be used.

    Severity: 7.3 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-5846 – GitLab EE GraphQL Framework Assignment Vulnerability

    June 26, 2025

    Encoding Explorer transforms characters into binary

    June 21, 2025

    CVE-2024-13955 – Aspect SQL Injection Vulnerability

    May 22, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.