In this post, I covered from minute 11:00 until 26:00 of this video.
I understand now why Laravel is so popular. My mind just blown after knowing
that you can create an authentication system that allow us to register user
and log in user very easily! In Laravel < 6 like the one the instructor is
using, he created it by using the command make:auth
. But in my version of
Laravel, first, we need to install laravel/ui package by running composer
require laravel/ui
. After it has been installed, when you run php artisan, you
will now see some new command are added to the list. Run php artisan ui:auth
. When
you refresh your app in the browser, the page changed and a link to Login and
Register page appear at the top right side. For now, we can't do anything just yet
because we still don't have database to create and store user information. We
will get to that later in this post. Even if you click on those 2 links,
somehow, it doesn't look too pretty...
php artisan ui bootstrap
and php
artisan ui vue
to install Bootstrap and Vue. For some reason, when installing
those two, my console doesn't show any installation progress so if you're
facing the same thing as me, it's not an error. You just need to wait a few
minutes until it says the installation is successful. After the installation,
you will notice that it ask you to run command npm install
and npm run dev
to
compile it. The instructor explained a bit about this.
npm -v
and node
-v
. Just like Composer, NPM is also a package manager, except that it is for
JavaScript. With NPM, there are some commands that we can run but to do that,
first we need to install dependencies in our project and that's the purpose of
why we need to run npm install
.
npm run dev
. What this will do is, it takes
everything that comes with Laravel, compile it to a file that we can actually
use. The reason we do this through webpack is because it able to compile quite
significant amount of code into the smallest minified file.
Let's create an empty database file named database.sqlite inside the
database directory with command vim database/database.sqlite
. At first, I
received an error like this:
vim is not recognized blabla.... To solve this error, I install Vim by downloading the self-installing
executable file (.exe) from the official site. After running that command, just exit out by writing :wq
and then
hit enter.
Next, we will explore the .env file that's located in the root. This file
holds all the configurations that are specific to your environment like
localhost, staging or production environment setup. If you look at the file,
you should have a localhost environment setup. However, my file is a bit
different from the instructor. In the video, the instructor has
APP_URL=http://localhost
but what I have is
APP_URL=http://mysite.test
. I don't know why my file is different
from him but I will leave it at that for now. The more important thing we
should do first is take a look at the database setup section.
Because we're going to use SQLite, we can go ahead and delete all the db
lines except for DB_CONNECTION
. By default, the db connection is set
to mysql so we need to change this to sqlite and then save the file. When we
make any changes to the .env file, we need to restart our development
server or else we will came across errors later. In the Laravel version that
the instructor is using, he has to restart it manually. To restart the
server, you need to stop it by pressing CTRL+C first, and then run serve
command again. But in the version I am using, whenever changes is made in
.env file, the server will restart itself. Just for your information.
Let's go back to the migration thing. So how we actually get the database
migrating? Or in other words, how do we get it to the latest state
possible? With that, of course we're going to use php artisan command.
Just run php artisan migrate
and you'll see that it created some tables.
There's a difference between my version and the instructor's version again.
The command create 2 tables for the instructor, but I have 3 tables. I
have a table named users
, password_resets
which are the same as the
instructor and also one extra table named failed_jobs
. The table name is
pretty self-explanatory. For table users
, of course it will store users'
data, table password_reset
for holding any password resets and failed_jobs
for storing any
failed job
attempts.
Now that we have database and tables, we can now register users in our
app! Weehoo! For the Name field, you can insert more than one sentence or
include special characters. It's just your display name, not your username
that supposed to be unique. This register process uses the email address
as the unique key so if you use the same email to register another user,
it will show an error. You should also be able to logout, and login now.
If you logged out and enter http://127.0.0.1:8000/home page, which is
the page that logged in users will be redirected to, you won't be able to
access it but instead, you will be redirected to the login page. All of
these has been set up for us because we run the ui:auth
command.
SUPERB!
That is all for part 3! I think this part contained more useful information than the previous part. In the next part, we will be doing my most favorite thing to do - designing the user interface!
Highlights ⚡
- Install laravel/ui package with
composer require laravel/ui
- Install Bootstrap with
php artisan ui bootstrap
- Install Vue with
php artisan ui vue
- Create authentication and session with
php artisan ui:auth
- Install NPM dependencies with
npm install
- Compile assets with
npm run dev
-
Create new SQLite database file with
vim database/databasename.sqlite
- Create tables for authentication with
php artisan migrate
- All of the front-end assets should be compile
- Development environment configuration is in .env file
- You can close development server either by closing the console, or pressing the CTRL+C
Helpful Links 🔗
- https://stackoverflow.com/questions/34545641/php-artisan-makeauth-command-is-not-defined
- https://www.itsolutionstuff.com/post/laravel-8-install-bootstrap-example-tutorialexample.html
- https://www.techiediaries.com/laravel/how-to-install-vuejs-in-laravel-6-7-by-example/
Special Section 🧠
<style></style>
tag in the view and paste it in
app.css. After that, I replace the style tag with <link href="{{
asset('css/app.css') }}" rel="stylesheet">
before compiling it. When I
refresh my app in the browser, it look just the same as before, which is a
good thing because it doesn't break despite the changes I made. So I
guess, if you don't use Bootstrap, you can write your own CSS in app.css
like this too.
No comments:
Post a Comment