Laravel run your first program


To run your first program in Laravel, you will need to follow several steps. Laravel comes with a default welcome page that makes setting up your first program quite simple. Here’s a step-by-step guide to getting Laravel up and running, and creating a basic route to return a view or a simple string.

Steps to Run Your First Program in Laravel

1. Install Laravel

If you haven't already installed Laravel, follow these steps to set up a new Laravel project.

  1. Install Composer: Laravel requires Composer to manage its dependencies. Make sure Composer is installed on your system. You can check this by running:

    composer -v
  2. Install Laravel via Composer: Run the following command to create a new Laravel project:

    composer create-project --prefer-dist laravel/laravel first-laravel-app

    This command will download and install a fresh Laravel project in a directory called first-laravel-app.

2. Navigate to Your Project Directory

After installation, navigate into your project directory using the command:

cd first-laravel-app

3. Run the Laravel Development Server

Laravel comes with a built-in development server. To start the server, run the following Artisan command:

php artisan serve

This will start the development server, and it will typically be accessible at:

http://127.0.0.1:8000

When you visit this URL in your browser, you will see the default Laravel welcome page, which indicates that your Laravel installation is successful.

4. Create a Simple Route

To run your first custom program, you’ll define a simple route in Laravel.

  1. Open the routes/web.php file in your Laravel project. This file is where you define routes that handle HTTP requests for the web interface.

  2. Add the following route to the web.php file:

    Route::get('/hello', function () { return 'Hello, World!'; });

    This defines a GET route that listens for requests to /hello and returns the string "Hello, World!".

5. Test Your Route

Now, go to your browser and visit the following URL:

http://127.0.0.1:8000/hello

You should see Hello, World! displayed on the page, confirming that your first Laravel program is running successfully.

6. Return a View

To make things more dynamic, let’s create a basic view and return that view instead of a plain string.

  1. Create a new Blade view file in the resources/views directory. For example, create a file named hello.blade.php:

    • Path: resources/views/hello.blade.php
    • Content:
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello, Laravel!</title> </head> <body> <h1>Hello, Laravel!</h1> </body> </html>
  2. Modify your route in web.php to return this view:

    Route::get('/hello', function () { return view('hello'); });
  3. Now visit the same URL (http://127.0.0.1:8000/hello) in your browser. You should see the HTML content rendered with "Hello, Laravel!" as a heading.


Summary of the First Program Workflow

  1. Install Laravel via Composer.
  2. Start the development server using php artisan serve.
  3. Define a basic route in routes/web.php that returns a string or view.
  4. Test the route in your browser.

You have now successfully run your first Laravel program! You can extend this by working with controllers, models, and other powerful features that Laravel offers.