Skip to content

Tim's Laravel Intro

Tim La Roche edited this page Jan 7, 2019 · 2 revisions

Laravel Guide (at least, what I know)

Hopefully with this document, you can get the working environment for our Laravel application running.

Introduction to the tools

We want to develop in an environment simliar to the environment we deploy in. These servers on the cloud are usually barebones and empty so that we can configure them exactly how we want. They provide flexibility and power.

Vagrant is a tool we will be using to provision (create) Virtual Machines (VMs). VMs are disposable and do not intefere massively with our file system.

Vagrant uses the concept of boxes, these are containers that have packages pre-installed so that two different users running the same box will have the exact same environment. Homestead is the name of the Laravel box.

Installing everything we need

Installing Vagrant (and a virtualiser)

Note: All these packages are binaries that are downloaded from the respective websites. I am not sure if they are available through package managers. Installing from a .deb file or a similar equivalent is relatively simple.

First, we need to install a virtualiser. This will handle the actual running of the VM. I have been using VirtualBox 5.2 and I reccomend we all use this.

We then need to install vagrant from the website. Vagrant will need to be added to your PATH (it may do this automatically in the installer) for the commands to work globally.

RSA Keys

If you haven't already, you will need to generate a pair of RSA keys at have them in the .ssh folder. See here for details.

Let me know if any issues with this part arise when you go through this.

Installing Homestead

Create a directory in your home folder ~ called Homestead. Navigate to this directory. It should look something like ~/Homestead.

When inside the folder, run this command.

git clone https://github.com/laravel/homestead.git ~/Homestead

Then run

git checkout v7.18.0

The first command clones the homestead repo and the second command makes sure we are using a stable branch version.

Finally, we need to run

bash init.sh

You may need sudo for this.

Navigate to the Homestead folder where we installed Homestead. Open the Homestead.yaml file and replace it with this

---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/team1code
      to: /home/vagrant/code

sites:
    - map: homestead.test
      to: /home/vagrant/code/Team1Helpdesk/public

databases:
    - homestead

# ports:
#     - send: 50000
#       to: 5000
#     - send: 7777
#       to: 777
#       protocol: udp

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# zray:
#  If you've already freely registered Z-Ray, you can place the token here.
#     - email: foo@bar.com
#       token: foo
#  Don't forget to ensure that you have 'zray: "true"' for your site.

Save our changes and then we have a few commands to run.

First, we need to make a folder in our home directory called team1code.

mkdir ~/team1code

Now, we can start! Navigate to our Homestead folder and run, vagrant up. This will go about creating and provisioning the VM. When the VM is up, we can ssh into it. Run vagrant ssh.

Usage

Vagrant SSH

By running Vagrant SSH, we are now 'inside' the VM. You should see a folder named code. This folder maps to the team1code folder in your home directory. Everything that you do inside team1code is reflected inside the VM automatically and vice versa.

While the VM is up, we will work inside the home directory. For the most part (as far as I know as of writing this) we will not need to concern ourselves with the folder inside the VM. As long as it reflects our home folder (which it should) then we will be good.

Working

Navigate to ~/team1code and clone the github repo of my laravel version of the work. We want to rename the folder we cloned to be called Team1Helpdesk and make sure that when we open the folder it goes straight to the source files and there is no interim folder i.e the path should be Team1Helpdesk/*.ext and not something like Team1Helpdesk/Team1Helpdesk-Master/*.ext.

Now, on a web browser, navigate to 192.168.10.10 and you should see our homepage!

Quick notes on working

From here, you can experiment and play around with Laravel and see what happens. After exams we can hopefully get a chance to talk and help each other out with learning things.

The main things to note are that Laravel uses a templating system with a framework called Blade. These are essentially html where you can do things such as replace the content of tags with variables that you pass in via PHP. Think about if we wanted to show the current logged in username in the top right corner, we can pass it into the template and render it serverside. (We could discuss rendering things client side however this may be a lot of work for the amount of content we have and the payoff.) You can find these files in resource/views.

An important folder is the routes folder. Inside here you will see a file called web.php. You can see two routes defined: / and /main. If you navigate to these (192.168.10.10/main) you will see that each route fires the function that defines it (oddly worded but I hope you understand). In this case, we return a view respectively.