Pages

How to install Laravel using composer and configure


In this article, we will guide you through the process of Laravel installation using composer and configuration of the web server. Install Laravel using Composer with the help of the following steps. 

Prerequisites 

The Laravel framework requires some prerequisites for the system. All these requirements you will get from Laravel Homestead virtual machine and we would highly recommend you to use Homestead as a local Laravel development environment. 


In case, you are not using Homestead, makes sure that your server meets the following requirements.

  • BCMath PHP Extension

  • PHP >= 7.2.5

  • Ctype PHP Extension

  • JSON PHP Extension

  • OpenSSL PHP Extension

  • Fileinfo PHP extension

  • PDO PHP Extension

  • XML PHP Extension

  • Mbstring PHP Extension

  • Tokenizer PHP Extension

Step 1: Install Laravel

With the help of Composer, you can install Laravel which also helps you manage dependencies. Therefore, before using Laravel, make sure that you have Composer in your system beforehand. 


Through Laravel Installer

First, you need to install a Laravel installer with the help of Composer.

composer global require laravel/installer


Ensure that you place the system-wide vendor bin directory of Composer in your $PATH so that your system can locate the Laravel executable. Now, this directory locates in various locations depending on your operating system; however, some of the common locations include:

  • Windows: %USERPROFILE%\AppData\Roaming\Composer\vendor\bin

  • macOS: $HOME/.composer/vendor/bin

  • GNU / Linux Distributions: $HOME/.config/composer/vendor/bin or $HOME/.composer/vendor/bin


You can also find the global installation path of the composer when you run composer global about and look up from the first line. 


In your recommended directory, laravel new command will build a new Laravel installation after the completion of the installation. 


Using the command laravel new blog, for example, will produce a directory called blog containing a brand-new Laravel installation with all of Laravel's dependencies already set up:

laravel new blog


Use Composer Create-Project

The alternative method to install Laravel is by using Composer create-project command in your terminal.

composer create-project --prefer-dist laravel/laravel:^7.0 blog


Local Development Server

In case you have already installed PHP in your system locally and want to use PHP’s built-in development server to server for your app, the best thing is to use the serve Artisan command. Now, this command helps in starting a development server at http://localhost:8000: 

php artisan serve

Step 2: Configuration

After Laravel installation, the next step is to configure the web root or document of your web server to be the public directory. The role of index.php file in this directory presents as a front controller for every HTTP request entering your app. 

Configuration files

All kinds of Laravel configuration files are stored in the directory named config. Each kind of option is documented and therefore you can look through the files and know all the options given to you. 

Directory Permission

You also need to configure a few permissions after Laravel installation. Directories within the storage and the cache or bootstrap directories should be writable by your Laravel or web server will not run. In case you are using the Homestead virtual machine, all these permissions were already given. 

Application Key

After installing Laravel, you should configure your application key to a random string. This key has already been set for you if you installed Laravel using Composer or the Laravel installer thanks to the PHP Artisan key: generate command.


This string should typically be 32 characters long. The.env environment file can be used to set the key. If you haven't already, you should copy the.env.example file to a new file with the extension.env. Your user sessions and other encrypted data won't be secure if the application key isn't set!

Additional Configuration

Almost no further setting is required with Laravel out of the box. You are free to start creating now! You might want to look at the config/app.php file's documentation, though. It has a number of settings that you might want to modify depending on your application, like timezone and locale.


You could also wish to set up a few extra Laravel components, like

Step 3: Web Server Configuration

Directory Configuration

When serving Laravel, your web server's "web directory" should always be the root. A subdirectory of the "web directory" should not be used to serve a Laravel application. If you do, your application's sensitive files might be made public.

Pretty URLs

Apache

With Laravel, you may offer URLs without the index.php front controller by using the public/.htaccess file. Enable the mod_rewrite module before serving Laravel with Apache to ensure that the server will respect the.htaccess file.


Try this alternative if the.htaccess file provided with Laravel does not function with your Apache installation:

Options +FollowSymLinks -Indexes

RewriteEngine On

 

RewriteCond %{HTTP:Authorization} .

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

 

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]


Nginx

All requests will be routed to the index.php front controller if you are using Nginx, according to the directive listed below in your site configuration:

location / {

   try_files $uri $uri/ /index.php?$query_string;

}





 

How to Build a RESTful API Using Node Express and MongoDB- Part 1

In this blog, we will build a Rest API using Nodejs, Expressjs, and MongoDB. This is the first part where we will talk about the basics of building REST APIs with the help of NodeJS, ExpressJS, and MongoDB.  

Step 1: Run a command

Run the below command in an empty folder:

npm init


After running the command, it will ask you to enter various details. You need to enter project names, the repository, the author, and much more. It will generate a file name package.json in the folder.  

                                                  Source: FreeCodeCamp.org


{

  "name": "rest-api-express-mongo",

  "version": "1.0.0",

  "description": "",

  "main": "index.js",

  "scripts": {

    "test": "echo \"Error: no test specified\" && exit 1"

  },

  "author": "",

  "license": "ISC"

}


The package.json file contains all the scripts such as how to test the application, and how to run the application, plus all the dependencies. This NodeJS REST API MongoDB you can built accordingly. 


Install some of the dependencies now:

npm i express mongoose nodemon dotenv

Step 2: Install and set up frameworks

  • Install NodeJS to restart the server every time you save the file.

  • ExpressJS is utilized as middleware to create multiple CRUD endpoints.

  • Mongoose is used for managing your data in MongoDB with the help of multiple queries. 

  • Dotenv helps you to manage a .env file. 


Make a file called index.js once they have finished installing. This will serve as our application's starting point.


Let's execute this file after adding Express and Mongoose.

const express = require('express');

const mongoose = require('mongoose');


Transfer all the Express contents into a new constant named app. 

const express = require('express');

const mongoose = require('mongoose');


const app = express();


const express = require('express');

const mongoose = require('mongoose');


const app = express();


app.use(express.json());


app.listen(3000, () => {

    console.log(`Server Started at ${3000}`)

})


After this, the server is set to Port 3000. In order to start our server we also need to add app.use. We have a piece of code that enables us to receive the JSON formatted data.


Include the following script in the package.json file:


"scripts": {

    "start": "nodemon index.js"

},


It also represents the way to start the server with the help of npm start, this will run with the help of Nodemon package that we installed earlier. 


Enter the npm in the terminal, and the following output will occur. 

                                               Source: FreeCodeCamp.org

Step 3: Configure the MongoDB Database

To configure the MongoDB Database, you need to log in to MongoDB or create your account, or sign in to the account. 


Sign in to the account, we need to create a database. 

                                            Source: FreeCodeCamp.org


Create a Free Shared Cluster.


You need to enter the username and password, so fill that information in. 

Now, add your IP Address.

                                                           Source: FreeCodeCamp.org


Press on the Finish button and close the window.

                                             Source: FreeCodeCamp.org


The process will take some time for the cluster to finish, so you need to wait. After this create a .env file in the folder. 


Now on the Home Page of Cluster, press on the connect button. 

                                              Source: FreeCodeCamp.org


The below image will appear:

                                                  Source: FreeCodeCamp.org


Press on the MongoDB Compass, and you will see the below string. Plus, install the MongoDB Compass.

                                               Source: FreeCodeCamp.org


This string that you have previously used should now include your username and password. The completed connecting string will resemble the following:


mongodb+srv://nishant:********@cluster0.xduyh.mongodb.net/testDatabase


The username, in this case, is ‘nishant’, the password comes next, and the database name comes last.


Paste this string into the.env file as a result.

DATABASE_URL = mongodb+srv://nishant:*******@cluster0.xduyh.mongodb.net/testDatabase


Add the string in MongoDB Compass. 


                                                Source: FreeCodeCamp.org


Now, press the connect button. 


You will get here two Databases by default. And later third one will be created automatically. 

                                                Source: FreeCodeCamp.org


After that, you need to import all the contents to .env file in the script file name index.js. 


require('dotenv').config();


const mongoString = process.env.DATABASE_URL


Now, we need to store the string into a mongoString variable. 


You need to connect the database to the server using Mongoose. 

mongoose.connect(mongoString);

const database = mongoose.connection


Now, depending on whether our database connection succeeds or fails, we must throw a success or an error message.

database.on('error', (error) => {

    console.log(error)

})


database.once('connected', () => {

    console.log('Database Connected');

})


Database.on in this case indicates that it will attempt to connect to the database and throw an exception if the attempt fails. And database.once indicates that it will only be run once. If it is successful, a notification that reads "Database Connected" will appear.

                                         Source: FreeCodeCamp.org


Below is the code from the start. This is the REST API example that will help you build it. 

require('dotenv').config();


const express = require('express');

const mongoose = require('mongoose');

const mongoString = process.env.DATABASE_URL;


mongoose.connect(mongoString);

const database = mongoose.connection;


database.on('error', (error) => {

    console.log(error)

})


database.once('connected', () => {

    console.log('Database Connected');

})

const app = express();


app.use(express.json());


app.listen(3000, () => {

    console.log(`Server Started at ${3000}`)

})


Make new Model/Controller/Migration in Laravel

  In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here w...