Getting Started With Laravel 4, Chapter 3: Your First Application

Sketching out the application

Our first app will be a browsable cat database.  We will leave authentication until next chapter.

Entities, relationships and attributes

The first thing to decide where we start a new application is our objects and their attributes. (i.e., our tables, columns and any relationships between them)

The map of our application

You should write out all the routes you need for your app.  You can then translate these into controllers.  Standard HTTP requests do not support the verbs, Laravel uses a method field on the the form to emulate these verbs.

Starting the application

In this section, we start building our new cats application.  Instructions are well written, and things just worked on my mac.  I did run into some issues, I was running Laravel 5, and a lot of things changed.  I would recommend getting a newer book, but this one was free, and the data is still good to learn Laravel.

Using the builtin development server

PHP 5.4 comes with a builtin server.  I had no issues running this.  If you are running Windows, you will need to follow these instructions.  The book contains some troubleshooting tips if you run into issues.

Writing the first routes

I'm writing this a few years after this book has come out.  The book discusses Laravel 4, and I'm using Laravel 5.  The routes have been moved from where the book listed them.  I found them in app/routes/web.php.  So far they everything has worked.

Restricting the route parameters

The author shows how to setup a where clause on the routes.  He does point out the pretty error message when you try to go to a bad route.  I didn't get this error at first.  As I followed along with the tutorial, I did start running into the error messages.  Something has changed between Laravel 4 and 5, but I couldn't determine what it was.  It didn't really matter at this point.

Catching the missing routes

This section discusses adding a 404 page.  I couldn't find where this was in Laravel 5.

Handling redirections

This code could be better explained.  You need to replace the first route you created with the example shown in this section.

Returning views

Laravel5 has moved views from the location in the book to resources/views.  Once you start creating the views in the right spot, the code works as shown.

Creating the Eloquent models

This is the first part where I started running into problems.  I had to do quite a bit of side work to figure out how to fix all this.  In the end I ended up using the command php artisan make:model 'table_name'  This seems to be the right way to do it.

Building the database schema

Again in Laravel 5, the command has changed it was php artisan make:migrate now it is php artisan make:migration.  I was a little disappointed in how the Eloquent ORM handles creating tables.  Maybe I didn't study it hard enough, but I was hoping it would be more like Django.  I ran into some issues trying to make run the migrate command.  I found out you also have to change the .env file.  As shown below.
```
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:z0RUXcvi42gNTiG/AOZzPqQmCCmFWiA7rhaNmEUdarw=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack


DB_CONNECTION=sqlite
DB_DATABASE=database/production.sqlite

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
```

After that it worked as described.  Note that the database file doesn't have to live  in your Laravel directory.  In fact in production, I wouldn't place it there, but there is no harm in leaving it there for testing purposes.

Links:
https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions
https://laravel.com/docs/5.0/migrations

Seeding the database

You can create a class inside the DatabaseSeeder.php file.  This class can load arrays, csv's and even JSON data. 

Mastering Blade

To output a variable in Blade, use {{$var}} or {{{$var}}}.  The triple braces will help protect agains XSS vulnerabilities.
The standard PHP  functions have been translated into
  • @foreach
  • @if
  • @elseif
This will avoid the need to open and close php tgs in your view.  When I first tried Laravel in the past, I didn't like this, but as I've grown as a programmer, I think this is a much better way of doing it.  If you ever need/want to work on a Ruby app, this will be familiar syntax for you.

Creating a master view

You can create a master view to handle the main layout of your site.  There is a lot here.  I recommend reading the Laravel docs.

The overview page

We are going to create an index page that will display the cat breeds.  We can reuse this view to show our filtered breeds.

Displaying a cat's page

The author starts with a long function and then goes on how to show how Laravel lets us build a more compact route by binding the model to the view.  He also shows how to build the veiew.  One issue I ran into is that in Laravel 5, you have to use namespaces for you models.  Instead of saying Cat, you have to say \App\Cat.  I think the models should probably live in their own special directory, but I was in a hurry to learn as much about Laravel as possible.  This is also a discussion that should be had in another time and place.

Adding, editing, and deleting cats

There is a lot of code here, and I never did get it to work fully on my machine.  I'm not sure if that was a problem with my local PHP development environment, or if it was a problem with trying to convert to Laravel 5. 

Comments

Popular posts from this blog

Go Programming Blueprints, Chapter 2, Adding User Accounts

Successful Big Game Hunting Chapter 10