DEV Community

Cover image for Installing Laravel and defining routes
Klim Semenov
Klim Semenov

Posted on

Installing Laravel and defining routes

So now, in this second post of building the Laravel website, we will get into actual coding.

We have 2 main tasks:

  • Installing Laravel on Homestead
  • Creating 3 view routes for our pages

You can use various local environments while doing Laravel coding. But I prefer Homestead.

Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine.

In this series, we will not cover the Homestead installation process but only use it.

Installing Laravel

If you didn't start Homestead, run in terminal homestead up and your pre-configured virtual machine will start.

For our website, you should configure the Homestead.yaml file. We are adding a new sync folder, site, and database.

folders:
    - map: ~/vagrant/laravel-catalog
      to: /home/vagrant/laravel-catalog

sites:
    - map: laravel-catalog.test
      to: /home/vagrant/laravel-catalog/public

databases:
    - laravel-catalog
Enter fullscreen mode Exit fullscreen mode

After that, for our changes to be applied, you need to run:
homestead reload --provision

One more step is needed for the correct work of our local website is adding an entry in your hosts file.

I do it in terminal on my mac machine by editing it with nano. Just type sudo nano /etc/hosts and add one line:

192.168.10.10 laravel-catalog.test
Enter fullscreen mode Exit fullscreen mode

And finally, you are ready to install the Laravel framework!

All you need is just go to your project folder and run:
laravel new

Now, if you go to http://laravel-catalog.test in your browser you should see the Laravel welcome page. Congratulations!

Let's enter some configuration to our .env file.

APP_URL=laravel-catalog.test

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=laravel-lidings
DB_USERNAME=homestead
DB_PASSWORD=secret
Enter fullscreen mode Exit fullscreen mode

I must mention, that it is more correct to enter APP_URL without http or https. Only the domain. Some Laravel admin panels need such form of configuration.

Defining Routes

Routes in Laravel are defined in route/web.php file.

Route::view('/', 'index');
Route::view('/catalog', 'catalog');
Route::view('/product/{code}', 'item');
Enter fullscreen mode Exit fullscreen mode

For now, we only use View Routes. Because we don't need any logic except displaying our 3 pages. In future posts, we will change this, of course.

The third route for the item detail page is more complex than others. We capture {code} segment in it. When adding logic to this one, we will use this code variable to get a specific product and display information about it.

Views for our Pages

Also, we need 3 views for these routes.

Views are stored in resources/views directory. We can delete the default welcome.blade.php (we don't need it).

And create 3 views:

  • index.blade.php
  • catalog.blade.php
  • item.blade.php

Just put some default text into them.

Views in Laravel use blade templating engine. We will cover it while moving to our final website.

That's it for the second post in our series. All project code is available on github.

Top comments (0)