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

Laravel – Blade Templates

Laravel – Blade Templates Laravel 5.1 introduces the concept of using  Blade , a templating engine to design a unique layout. The layout thus designed can be used by other views, and includes a consistent design and structure. When compared to other templating engines, Blade is unique in the following ways − It does not restrict the developer from using plain PHP code in views. The blade views thus designed, are compiled and cached until they are modified. The complete directory structure of Laravel is shown in the screenshot given here. You can observe that all views are stored in the  resources/views  directory and the default view for Laravel framework is  welcome.blade.php . Please note that other blade templates are also created similarly. Steps for Creating a Blade Template Layout You will have to use the following steps to create a blade template layout − Step 1 Create a layout folder inside the  resources/views  folder. We are...

What is Laravel Framework?

What is Laravel Framework? Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern.

What is routing and how, and what are the different ways to write it?

What is routing and how, and what are the different ways to write it? All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php are stateless and are assigned the api middleware group. For most applications, you will begin by defining routes in your routes/web.php file.