In the first episode we will have a look at what tools we need or actually what I use as a Laravel developer. Im working with Windows mostly, so to cover the basic requirements like PHP and databases, I just use Xampp to host thing locally and my editor is VS Code.
I have a habit of storing my projects inside \xampp\htdocs\laravel\NameOfProject
even tho its compltely up to you where to store your project files. But before we get to actually work with laravel we have to install composer which is a PHP dependency manager. Just download it and follow the install wizard. Once composer is installed open your command prompt and type this command to globally install laravel:
composer global require laravel/installer
Once the installation is complete we are ready to create our laravel project by just typing the command:
laravel new NameOfProject
Or if you want to go with the composer way:
composer create-project laravel/laravel
In the mean time while our project is being created we can quickly jump to phpmyadmin and create a database which we will use for our application.
After its done, we can navigate to the project folder, in my case I named it “Hello”. Let’s open it with VS Code and have a quick look at the project structure.
It should be pretty self explanatory what each folder holds. But let’s first run our laravel application by typing into the terminal (I use the integrated terminal in VS Code):
php artisan serve
In the terminal you should see that the development server has started, just copy the url to your favorite browser (in my case chrome) and you should see the initial pre-generated Laravel welcome page with links to their homepage, documentations and a bunch of other useful resources. But let’s get back to our editor and have another look at the project structure. In this episode we will mainly focus on two directories /resources/views and /routes.
In the views folder we will edit the current welcome.blade.php which we will use as our home page. If you noticedthe tempaltes in laravel are structured by the template name, followed by .blade and then .php
Blade is laravel’s templating engine which we will have a more in depth look in later episodes.
So lets open up the files welcome.blade.php and remove everything inside the body tags and add <h1>Hello, world!</h1>, now if we refresh we should see Hello, world! in the browser.
Lets create a new view called about.blade.php and just create the basic html structure with <h1>About</h1> in the body tags.
Now that we have our views ready, lets create the required routes so that our views can get served as pages when we enter a specific url. Lets open web.php inside the /routes folder. As we can see the root “/” path is already defined with the welcome view being served. Lets do the same and use the /about path as our route and the about template as the page view.
Now if we navigate to /about in our browser we should see “About!” written on the page.
Routes are structured in a way that we define the route followed by the request method, in this case “get” which takes in two parameters so to say, the path and the function it executes when that path is being requested and served. The function in our case return the about view.
For the next episode:
I would leave it here for this episode. And let you try and play around a bit. Try to create a new view call “Diary” and create a new route with the path “/diary” till the next episode where we will dig deeper into more advanced features. Should be really fun when we connect to a actual database and use controllers to make the application truly dynamic.
Throughout this series we will build a fully functional web app by covering everything you need to start developing in laravel. And as its almost my moto at this point, we will also learn how to learn new things when we encounter something new — as a hint, our best friend will be the laravel official documentation. So keep practicing even the basics and build up as we progress through the series.