Dec
192020
- Posted By TheGrow Apps
- Productivity
Building Web Applications From Scratch With Laravel
Do you have any interest in building web applications? Are you looking to create your own website? You have come to the right place.
Here you can get all the specific knowledge required in making a Laravel based web application. Certainly, your qualification/education has no role to play in the world of information technology. If you have the potential, you can learn any language and become a wonderful developer.
Below, I have shared the specific process through which any fresher/intermediate can develop his/her Laravel web application.
Why Laravel ?
There are so many web application frameworks such as Cake PHP, Ruby On Rails, Symfony, Django etc. But, when it comes to syntax, expressions, Laravel always becomes the perfect option. The authentication, architectural pattern offered by Laravel saves a lot of time. Laravel also offers its specific command line that makes installation of libraries quite easy.
Laravel works on the Modal-View-Controller approach. Therefore, if you have worked on any MVC framework, you can easily learn Laravel. Moreover, Laravel is based on class and object concepts. The basics of any object oriented language can help you develop your Laravel web application in very less time.
Prior Knowledge
If you don't have specific knowledge about Laravel web framework, at least you should know the basics of core php. You need to know how to make a connection with the database and run queries. Well, if you know any php web application framework, then also you are ready to build your first web application.
However, if you don't have any knowledge of core php, you need to go through videos or articles that provide basic knowledge of php. There are so many online courses, videos that can make you well versed with any php based web application framework.
Environmental Set Up
Database - Well, you need to install a database in your system. If you don't have knowledge of any database, i would suggest you to install mysql. Through the online steps, you can easily install adminer database in your system. However, if you know any other database, you can install it and connect with the development program.
Php Unit - After the database, php unit is another thing to be installed in your system. Online, you search the version of the php. With the latest php version, you can perform several tasks in quicker periods.
Node JS - This is not a mandatory step. Well, if you have knowledge of Node.js and want to work with javascript, you can download Node.js from the internet.
How To Build Web Application With Laravel
Now, if you got specific knowledge of Laravel and installed specific tools, you can start creating your web application.
Step 1 - Download Laravel - From the internet, you should download Laravel. But, you will not get not success if the php version in your system is below 5.3. If you are on windows, you need to add PHP.ini extension for finishing the configuration. Last, you need to check if there is a library called Mcrypt. This pre-installed library is useful for hash generation and encryption purposes.
If you are done with all these things, you are good to go. After downloading Laravel, you should download latest jQuery and twitter bootstrap from particular websites. These libraries are very much useful in improving the user interface and quicker actions inside web pages.
Step - 2 Understanding the Folder Structure - After the installation process, you should look for the public folder that contains several important files. If you have Laravel v3.2.1, you will see three folders css, js and img. And, if you have older Laravel, you should create these three folders
Then, you should move all the assets/images in the img folder. The styles or .css files should be placed inside the css folder. The remaining javascript files should be placed inside the js folder.
Step - 3 Configure Urls, Encryption Key - Unlike other php web application frameworks, Laravel specifically requires an encryption key. This encryption thing is required for managing the cookies of the browser. You need to search for a file called application.php. If you go to application folder, go to config and inside config folder, there is a .php configuration file.
Search for key, and make it blank. Now, you are required to create your own artisan key. Go to Laravel’s root directory and type - php artisan key:generate.
This command will generate a 32 character encryption key for Laravel. Now, copy this key and paste it against the key in application.php. Now, you have to make configuration in the main file called index.php.
For enabling pretty urls, you should make the index setting to blank. For making sure that this activity will work fine, you mod_rewrite enabled on your local web server such as Apache.
Step - 4 - SetUp A Virtual Host - Although, you can start the development process if you have the particular web hosting. By uploading the necessary folders in your web server, you can start working on your project.
Else, you are required to set up a virtual host for using Laravel. For making sure that things work well in the production environment. The necessary step is that you should not allow access to several application files and the Laravel library. You also need to make sure that your domain (whatever it is) should point to the public folder.
Below is the code that can help you to create a virtual host for your Apache server.
<VirtualHost *:80>
ServerName instapics.com
DocumentRoot "D:/Development/htdocs/instapics/public"
<Directory "D:/Development/htdocs/instapics/public">
</Directory>
</VirtualHost>
Then, you are required to add the domain to several host files. If you are using linux, you should open /etc/hosts. And, if you have the windows system, you should open . C:\Windows\System32\drivers\etc\hosts
Then add this line -
127.0.0.1 instapics.com or 127.0.0.1 (your domain).com
This will tell your system to resolve to local server of your system
Step - 5 SetUp Routing - Now, you need to set up routing for your web application. Specific functions called routes are responsible for handling all the certain requests of the web application. The URL’s are characterized by the routes.
If you write this code -
Route::any('home', function()
{
return View::make('home.index');
})
It will render the (yourdomain.com)/home e.g https://www.parentalbabycare.com/home. Then if you want to render any other thing, you need to change path in Controller i.e.
Route.Controller(‘whatever your path’).
An important difference in Laravel web application framework is that it does not route to default controllers.
It means that we can easily create several pages without having to change the complete configuration. Below is the example for creating an about us page using Laravel :
Route::any('about-us', function()
{
return View::make('home.about-us');
})
Yes, there is no need for further customisation, we can get the normal view with these 4 lines. Rest, if you want further changes, you can make it in the controller.
There are some more things you can do with Routes used in Laravel.
You can give a wildcard or change the particular url using these attributes.
HTTP verbs are another thing that can change the specific route. You can use HTTP verb to any type of url. So, you change the POST request to a GET request.
Filters - Filters are attributes that extend the overall functionality. You can check that a particular URL is appropriate or not. Based on that, you can open the link. If you have used hooks in CodeIgniter, you will find similarities while using filters.
Well, you can use these routing techniques in different conditions or all three at one time. These techniques makes laravel a wonderful web application framework.
Step - 5 Create Your Laravel Controller - The entire application is controlled by special components called controllers. Inside the application folder, you can find a folder called controllers. Whatever the name of your function, just create a file of that name. For example, if you want to create a login controller, write
class Login_Controller extends Base_Controller
{
public function action_index()
{
//do our login mechanisms here
echo 'test'; //echo test so we can test this controller out
}
}
Now, if you want to see ‘test’, just open the url “yourdomain.com/login”. Here you can write all the logic for your login file.
Then, you can create filters, open specific templates/views from your controller.
Wrapping Up
Whether you are a beginner or have specific php knowledge, you can easily build your first Laravel based web application. You need to know the basics of core php or any other php web application framework.
After installing php, Laravel, you need to structure the folders for libraries. After creating a virtual host, setting path of index.php, creating routes, you can write logic for your controller. Know your requirements and build the wonderful application that can operate on different devices.