Skip to main content

Laravel – Namespaces

Laravel – Namespaces

Namespaces are used in various programming languages to create a separate group of variable, functions and classes. A program may contain various functions and this may result in conflict with existing functions. Namespaces play a key role in avoiding such conflicts. This chapter gives you a detailed knowledge of namespaces and their usage in Laravel.
Declaration of namespace
Namespaces can be defined as a class of elements in which each element has a unique name to that associated class. It may be shared with elements in other classes.
You can declare a namespace as shown in the syntax given below −
Please note that the use keyword allows the developers to shorten the namespace. The default namespace used in Laravel is app, however a user can change the namespace to match with a web application.
You can create a user defined namespace by using artisan command as shown below −
On giving the above command, you can observe the output as shown in the following screenshot −
select_git
select_git
The namespace once created can include various functionalities which can be used in controllers and various classes. The code created on the basis of namespace in controller and kernel with files namely app/console/kernel.php and app/Http/controller.php are shown below −

Kernel.php

Note that the functions namely schedule and commands helps in scheduling methods for Cron jobs and other functionalities.

Controller.php

The controllers act as an intermediary between models and views. For the namespace which we created called Tutorialspoint, they will be used in core file of controllers controller.php. The namespace is initialized properly with Http\Controllers.
The namespace once created uses various other namespaces like AuthorizesRequests, DispatchesJobs and ValidatesRequests as mentioned in the above code.

Use Keyword

Namespaces take place in the position of current class. As mentioned in our example we have declared Tutorialspoint as our namespace and it is located in the app folder. The namespace declared will be App\Tutorialpoint. Whenever you want to use that class, you should use the use keyword.
The syntax for using use keyword is shown here −

Popular posts from this blog

What is Lumen?

What is Lumen? Lumen  is PHP micro-framework that built on Laravel’s top components.It is created by Taylor Otwell. It is perfect option for building Laravel based micro-services and fast REST API’s. It’s one of the fastest micro-frameworks available. You can install Lumen using composer by running below command 1 composer create - project -- prefer - dist laravel / lumen blog

Laravel – Response

Laravel – Response A web application responds to a user’s request in many ways depending on many parameters. This chapter explains you in detail about responses in Laravel web applications. Basic Response Laravel provides several different ways to return response. Response can be sent either from route or from controller. The basic response that can be sent is simple string as shown in the below sample code. This string will be automatically converted to appropriate HTTP response. Example Step 1  − Add the following code to  app/Http/routes.php  file. app/Http/routes.php 1 2 3 Route :: get ( '/basic_response' , function ( ) {    return 'Hello World' ; } ) ; Step 2  −  Visit  the following URL to test the basic response. 1 http : //localhost:8000/basic_response Step 3  − The output will appear as shown in the following image. Attaching Headers The response can be attached t...

Which template engine Laravel use ?

Which template engine Laravel use ? Laravel uses Blade Templating Engine. Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.