Author: saqibkhan

  • What is HTTP middleware?

    HTTP middleware is a technique for filtering HTTP requests. Laravel includes a middleware that checks whether application user is authenticated or not.

  •  Define composer.

    It is an application-level package manager for PHP. It provides a standard format for managing PHP software dependencies and libraries.

  • What is Laravel?

    Laravel is an open-source widely used PHP framework. The platform was intended for the development of web application by using MVC architectural pattern. Laravel is released under the MIT license.

    Therefore, its source code is hosted on GitHub. It is a reliable PHP framework as it follows expressive and accurate language rules.

  • File Uploading

    Uploading Files in Laravel is very easy. All we need to do is to create a view file where a user can select a file to be uploaded and a controller where uploaded files will be processed.

    In a view file, we need to generate a file input by adding the following line of code.

    Form::file('file_name');
    

    In Form::open(), we need to add ‘files’=>’true’ as shown below. This facilitates the form to be uploaded in multiple parts.

    Form::open(array('url' => '/uploadfile','files'=>'true'));
    

    Example

    Step 1 − Create a view file called resources/views/uploadfile.php and copy the following code in that file.

    resources/views/uploadfile.php

    <html>
       <body>
    
      &lt;?php
         echo Form::open(array('url' =&gt; '/uploadfile','files'=&gt;'true'));
         echo 'Select the file to upload.';
         echo Form::file('image');
         echo Form::submit('Upload File');
         echo Form::close();
      ?&gt;
    </body> </html>

    Step 2 − Create a controller called UploadFileController by executing the following command.

    php artisan make:controller UploadFileController --plain
    

    Step 3 − After successful execution, you will receive the following output −

    UploadFileController

    Step 4 − Copy the following code in

    app/Http/Controllers/UploadFileController.php file.

    app/Http/Controllers/UploadFileController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    
    class UploadFileController extends Controller {
       public function index() {
    
      return view('uploadfile');
    } public function showUploadFile(Request $request) {
      $file = $request-&gt;file('image');
      //Display File Name
      echo 'File Name: '.$file-&gt;getClientOriginalName();
      echo '&lt;br&gt;';
      //Display File Extension
      echo 'File Extension: '.$file-&gt;getClientOriginalExtension();
      echo '&lt;br&gt;';
      //Display File Real Path
      echo 'File Real Path: '.$file-&gt;getRealPath();
      echo '&lt;br&gt;';
      //Display File Size
      echo 'File Size: '.$file-&gt;getSize();
      echo '&lt;br&gt;';
      //Display File Mime Type
      echo 'File Mime Type: '.$file-&gt;getMimeType();
      //Move Uploaded File
      $destinationPath = 'uploads';
      $file-&gt;move($destinationPath,$file-&gt;getClientOriginalName());
    } }

    Step 5 − Add the following lines in app/Http/routes.php.

    app/Http/routes.php

    Route::get('/uploadfile','UploadFileController@index');
    Route::post('/uploadfile','UploadFileController@showUploadFile');
    

    Step 6 − Visit the following URL to test the upload file functionality.

    http://localhost:8000/uploadfile
    

    Step 7 − You will receive a prompt as shown in the following image.

    Browse File
  • Cross-Platform Integration

    Laravel is designed to work seamlessly with various technologies and services, such as cloud storage (e.g., AWS S3), caching systems (like Redis), and payment gateways (like Stripe). This flexibility makes it suitable for a wide range of applications.

  • Laravel Telescope

    Telescope is a debugging and monitoring tool for Laravel applications that provides insights into requests, exceptions, logs, and database queries. It helps developers troubleshoot issues and optimize application performance during development.

  • Validation

    Validation is the most important aspect while designing an application. It validates the incoming data. By default, base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.

    Available Validation Rules in Laravel

    Laravel will always check for errors in the session data, and automatically bind them to the view if they are available. So, it is important to note that a $errors variable will always be available in all of your views on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used. The following table shows all available validation rules in Laravel.

    Available Validation Rules in Laravel
    AcceptedActive URLAfter (Date)
    AlphaAlpha DashAlpha Numeric
    ArrayBefore (Date)Between
    BooleanConfirmedDate
    Date FormatDifferentDigits
    Digits BetweenE-MailExists (Database)
    Image (File)InInteger
    IP AddressJSONMax
    MIME Types(File)MinNot In
    NumericRegular ExpressionRequired
    Required IfRequired UnlessRequired With
    Required With AllRequired WithoutRequired Without All
    SameSizeString
    TimezoneUnique (Database)URL

    The $errors variable will be an instance of Illuminate\Support\MessageBag. Error message can be displayed in view file by adding the code as shown below.

    @if (count($errors) > 0)
       <div class = "alert alert-danger">
    
      &lt;ul&gt;
         @foreach ($errors-&gt;all() as $error)
            &lt;li&gt;{{ $error }}&lt;/li&gt;
         @endforeach
      &lt;/ul&gt;
    </div> @endif

    Example

    Step 1 − Create a controller called ValidationController by executing the following command.

    php artisan make:controller ValidationController --plain
    

    Step 2 − After successful execution, you will receive the following output −

    ValidationController

    Step 3 − Copy the following code in

    app/Http/Controllers/ValidationController.php file.

    app/Http/Controllers/ValidationController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    
    class ValidationController extends Controller {
       public function showform() {
    
      return view('login');
    } public function validateform(Request $request) {
      print_r($request-&gt;all());
      $this-&gt;validate($request,&#91;
         'username'=&gt;'required|max:8',
         'password'=&gt;'required'
      ]);
    } }

    Step 4 − Create a view file called resources/views/login.blade.php and copy the following code in that file.

    resources/views/login.blade.php

    <html>
       
       <head>
    
      &lt;title&gt;Login Form&lt;/title&gt;
    </head> <body>
      
      @if (count($errors) &gt; 0)
         &lt;div class = "alert alert-danger"&gt;
            &lt;ul&gt;
               @foreach ($errors-&gt;all() as $error)
                  &lt;li&gt;{{ $error }}&lt;/li&gt;
               @endforeach
            &lt;/ul&gt;
         &lt;/div&gt;
      @endif
      
      &lt;?php
         echo Form::open(array('url'=&gt;'/validation'));
      ?&gt;
      
      &lt;table border = '1'&gt;
         &lt;tr&gt;
            &lt;td align = 'center' colspan = '2'&gt;Login&lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
            &lt;td&gt;Username&lt;/td&gt;
            &lt;td&gt;&lt;?php echo Form::text('username'); ?&gt;&lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
            &lt;td&gt;Password&lt;/td&gt;
            &lt;td&gt;&lt;?php echo Form::password('password'); ?&gt;&lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
            &lt;td align = 'center' colspan = '2'
               &gt;&lt;?php echo Form::submit('Login'); ?  &gt;&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/table&gt;
      
      &lt;?php
         echo Form::close();
      ?&gt;
    </body> </html>

    Step 5 − Add the following lines in app/Http/routes.php.

    app/Http/routes.php

    Route::get('/validation','ValidationController@showform');
    Route::post('/validation','ValidationController@validateform');
    

    Step 6 − Visit the following URL to test the validation.

    http://localhost:8000/validation
    

    Step 7 − Click the “Login” button without entering anything in the text field. The output will be as shown in the following image.

    Login
  • First-Class Support for Queues

    Laravel’s queue system allows developers to defer processing time-consuming tasks (like sending emails or processing uploads) to a background job. This improves application performance and responsiveness, enabling smoother user experiences.

  • Event Broadcasting

    Laravel supports event broadcasting, allowing real-time event handling in applications. This feature is useful for building applications that require real-time updates, such as chat applications or live notifications, and can be implemented using WebSockets.

  • Localization Support

    Laravel includes robust localization support, allowing developers to create applications that can be easily translated into multiple languages. This is facilitated through language files, enabling developers to manage translations efficiently.