Friday, April 22, 2022

Laravel #1 | Intro + Install (w/o composer)

Composer

A composer is a dependency manager.

A dependency manager, for example, if we want to create a login system, we want to use the already existing library and not do anything from scratch. A composer helps us manage, install and load the library into our text editor (VSCode/Sublime etc.). We can use PHP Monolog in the composer library to create the login system.

We can install as many as we want and composer will help manage them.

Try to run Composer

  • In VSCode > Open any project > Terminal menu > New Terminal > type composer then click enter.
  • You'll see something like this:

There are 2 ways of using Laravel, first, we use a composer to create a Laravel project. Second, we use the composer to install the Laravel. It is better to install Laravel (latter) and use it without the composer.

  • In the terminal, type, composer global require laravel/installer. Hit enter.
Now, we can start using it.
  • Try creating a new project. Type, laravel new test-project.

  • Go into your project folder, cd test-project. Then, run php artisan serve.
  • Copy the link provided, go to your preferred browser and paste them. You'll see the Laravel page as below:

Environment Variables

  • In VSC, open .env file. (ctrl+p to search). By default, it will generate this file.
  • For all of the projects that you make, you'll find .env.example file.
  • .env is a private file (contains secret key etc.) and we do not want to share that info. We are not going to commit this file into repo.
  • We'll commit .env.example instead. Others can see the example but will not know the real value that we have in .env .
  • Let's say if we introduce a new variable, we'll make the changes in .env.example and commit that file. Others can just copy the changes from .env.example that they get (pull), and paste to their own .env file.

Rollback

  • To be used if you no longer wish to add the changes you made. (revert/down)
  • Type in php artisan migrate:rollback.

Routes

  • In VSCode > ctrl+p > search routes/web > click web.php.
  • By default, there are 4 different types of routes (see explorer bar).
  • We have API (usually we open our API for other people to connect. They're going to consume, while we provide some data.) and SPA/WEB (backend/frontend. In web, they're combined. In spa, they work separately.).

HTTP Method

  • HEAD
  • GET = to fetch data. Read, not write.
  • POST = write. To insert.
  • PUT = write. To update. More common than patch.
  • PATCH = write. To update.
  • DELETE = write.
  • OPTIONS = write. To update, but with attachments.

To test, again in VSCode > web.php > type in:
Route::get('/me', function () {
    return "Hello World!";
});
Then, go to your browser and add '/me' to the (previously opened) Laravel page url bar.

PHP Callback Functions

  • A function that is passed as an argument into another function.
  • Any existing function can be used as a callback function.
  • Used for easy implementation (instead of controller), for example, to redirect.

Models and Migration

  • In the terminal, try typing in php artisan. We're not going to use all of the commands. It depends on what you're trying to make.
  • To create a model, type php artisan make:model --help. Every command that we want to run, it'll have --help, -h, /? or ?.
  • You can also do php artisan make:model ModelName. Hit enter.
  • For controller, type php artisan make:controller ContName and hit enter.
  • Open the file. ctrl+p > ModelName.php.
  • You can explore more on PHP traits.

Namespace

  • As you can see in the ModelName.php, there is App\Models;
  • namespace is like a path. If we require the files, we need to specify the path.
  • Try creating a new folder in app > "Service/Admin". (skip this part if you want).
  • Then, create a new file inside Servic/Admin > AdminService.php.
  • The namespace will be:
  • Try typing in as shown below:


Now, we're going to try with the database (I use SQLyog).

  • In SQLyog, create a new database. Database > create database.
  • In .env.example, put your database name:
  • In the database that you just created, open table 'User' (right click and refresh, if you can't find it) and add dummy values (just for testing). Don't forget to save them.
  • Go back to your routes, web.php. (*Note that we don't need any functions). Add use App\Models\User after use Illuminate\Support\Facades\Routes.
  • Also add:
  • Now, refresh your browser and you'll see something like this:

Done! Try and explore more 👋

No comments:

Post a Comment

Komen atau sebarang pertanyaan di sini :)

WordPress #3 | How to Change Website URL

Hi. Today we're gonna look at how to change your website URL in case you messed up. In this case, yes, I messed up. That is why this pos...