Laravel URL Shortener - Become a Master in Laravel - 30

Laravel URL Shortener - Become a Master in Laravel - 30

19.Jul.2021

as normal you go in this lecture we will
complete this URL shortner project where
we will provide our small interface to
the user where the user will enter a web
link and we will convert that their
playing into the short URL so on click
of the short URL it will redirect to the
original URL so let's start making the
project here we have the route file
let's create a new route for this we are
starting an entire new project here with
the new URL route get short and we will
return a view URL dojo this URL will be
for providing the interface so that the
user can enter any URL to convert it in
short order and the another method will
be a post method of this URL where we
will get that post request of the URL
and will convert the URL in here so
let's first design our table PHP artisan
make migration URL short
it created a migration file for us let's
create a table here
ischaemic reared URL short function
blueprint and tape
the first field will be as usual
increments and ID now we will take two
fields first field will the URL where we
will store the original URL and the
second field will be the short URL and
we will take seven characters or we can
change this seven to five and the table
engine will be in no DB so this is our
table and let's complete this down
function drop if exist URL shop we have
created this migration let's add one
more field timestamp we have completed
the migration let's migrate this it
created the table let's check the
database here is the URL short able so
now let's create a model for this table
PHP artisan make model undal models
folder URL short
[Music]
let's create this model first we will
define our table name our table name is
URL short and as usual I also define
primary key so the protected primary key
which is ID
now we will define fillable here the
fields which are required to be filled
there are two fields first is URL and
the second is short so we have completed
our model
and let's first design our URL short we
are first view our first interface for
the user to convert URL let's create a
folder URL and then a file short dot
play dot PHP let's start with heading
charge PRL and we will provide a form a
post form and the action URL will be
short
now let's take an input book name URL
NID is also the URL
and then a submit button with short URL
[Music]
and don't forget to add this CSRF field
now let's check this view file and
here's the headed URL controller does
not exist
[Music]
I forgot to add posed here so now here's
the interface
now let's complete this post method for
this we will create a controller let's
create a controller PHP artisan make
controller open the controller let's
create the method public function char
and we will get the post request so the
request first we will turn this request
here we will use URL short models will
include this now let's check that and
here is the result now what we have to
do in this short method
first we will check that the URL exists
in the table or not if the URL already
exists in the database then we will show
the short URL and if the URL does not
exist in the database then we will
create a short URL and then save it to
the table and then show the short URL to
the user
so let's start with getting the URL
first we will get the URL URL short we
are URL and we will give this URL
request URL and we will get the first
and now for checking this URL we will
say that the URL is mod it means that
the URL does not exist in the database
because if the URL exists and this
recall will not be not so if we don't
have the URL then we will generate a
short URL we will create this method
later generate short URL and after
getting the short URL then we will store
this URL in the table URL short create
and we have two fields URL and here we
will restore the URL from the request
and then the short which we will get
from the generate short URL and after
saving the short URL then we will
retrieve that short URL
using vir URL and then we will provide
the request URL and then we will get the
first record and then we will return
this view with the data
[Music]
and if we already have the URL then we
will simply return the view with URL
object so first we will get the URL from
the table and if there is no such URL
then we will generate a new short URL
and after generating the URL we will
save this URL and then we will get that
URL from the table and then return this
object to this view file and we can also
do this we can remove all these
statements and then just return at the
end this view with the URL object so now
let's create this method generate short
URL result is is equal to base convert
then we will generate random numbers
between thousand to nine nine nine nine
these are five prime nines as we have
given short URL max length five and then
ten and thirty six
digits in number with a base higher than
ten will be represented with the letters
A to Z with a meaning 10 B meaning 11
and Z meaning 35 we will get the data
and we will check that if there already
exists the generated URL then we will
regenerate the URL and then finally we
will return the result so first we will
create a new short URL then we get the
URL from the table and if there already
exists a short URL with the generated
URL name then we will regenerate the
short URL and after that we will return
that short URL q8 this view file the
short URL so let's create this view
under views short URL don't play dot PHP
[Music]
we'll simply display a message click the
link and we will provide a link here a
new route having the link and the target
will be blank so we will open this in a
new tab and we will display the short
URL here now let's first check this
and we have a narrow use upon defined
constant result we have made some
mistake and here it is you forgot to add
dollar sign and defined URL short it
means that yeah here it is remove the
dollar sign and it generated the short
URL let's click this URL and it is
saying that there is no such doubt and
in the database let's check this we have
here a short URL all this we will create
this route
[Music]
and we will name this link and the short
link now create this method under URL
controller
and in URL controller we will create
this method short chain and we will get
the link we will get the entire raw by
using this link URL short we are short
link and the first and then we will
return redirect to URL that's it
now let's go and check this try to
region rate it will not regenerate as
this link is already exist and when we
click the link it open the URL let's try
another one
thank you very much for watching if you
want to check out more lessons like this
one then subscribe to this channel and
hit the like button see you in the next
lecture take care bye bye