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
Comments
Post a Comment