Laravel passport create API for your application

In the previous article Laravel api integration & configuration, we have successfully set up a Passport package in a fresh Laravel application. Now in this article, we are going to create API using the passport and we will protect all API routes and can be accessed If the user passes bearer token in each API request, otherwise the user can’t get any response.

build api using laravel passport

Now before we jump to build API using the Laravel passport package, we need to create a movie table and that will be used for the movies listing API and only accessible via a bearer token so let’s create a migration and seeder for that.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Step 1:

php artisan make:migration movies

Add below schema code to your movies migration file which will be placed in your database/migrations directory.

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('movies', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('year');
        $table->string('duration');
        $table->string('type');
        $table->string('about');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('movies');
}

Run migrate command to generate movies table.

php artisan migrate

To generate a seeder, execute the below code on your terminal screen.

php artisan make:seeder MoviesSeeder

Now add some dummy movies data in your MoviesSeeder seeder file which will be used for listing API.

<?php

use Carbon\Carbon;
use Illuminate\Database\Seeder;

class MoviesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('movies')->insert([
            [
                'name'       => 'The Shawshank Redemption',
                'year'       => '1994',
                'duration'   => '142 mins',
                'type'       => 'Drama',
                'about'      => 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.',
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now()
            ],
            [
                'name'       => 'Forrest Gump',
                'year'       => '1994',
                'duration'   => '142 mins',
                'type'       => 'Drama, Romance',
                'about'      => 'The presidencies of Kennedy and Johnson, the events of Vietnam, Watergate, and other history unfold through the perspective of an Alabama man with an IQ of 75.',
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now()
            ]
        ]);
    }
}

Don’t forget to include MoviesSeeder in your DatabaseSeeder class file.

Run seeder command to generate dummy entries.

php artisan db:seed

Step 2:

Now we are going to work on the following API so follow the code step by steps and implement in your application:

  • Registration
  • Login
  • Movies List
  • Logout

Create an API folder at app/Http/Controllers directory and generate the following controllers:

  • AuthController
  • MoviesController

Step 3:

Open your api.php file from the routes directory and update login, registration, movies & logout API routes.

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::group([
    'prefix' => 'auth'
], function () {
    Route::post('login', 'API\AuthController@login');
    Route::post('register', 'API\AuthController@register');

    Route::group([
      'middleware' => 'auth:api'
    ], function () {
        Route::get('movies', 'API\MoviesController@getMovies');
        Route::get('logout', 'API\AuthController@logout');
    });
});

Movies [app/Movies.php]

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Movies extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'movies';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'year', 'duration', 'type', 'about', 'created_at', 'updated_at'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];
}

AuthController [app/Http/Controllers/API/AuthControllers]

<?php

namespace App\Http\Controllers\API;

use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class AuthController extends Controller
{
    protected $user;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Login user and create token.
     *
     * @param Request $request
     * @return void
     */
    public function login(Request $request)
    {
        // Validate incoming request
        $validator = Validator::make($request->all(), [
           'email'    => 'required',
           'password' => 'required'
        ]);

        if ($validator->fails()) {
            $data = [
                'data'    => '',
                'message' => $validator->messages()->first(),
                'status'  => 0
            ];
        } else {
            if (!Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
                $data = [
                    'data'    => '',
                    'message' => 'Unauthorized.',
                    'status'  => 0
                ];
            } else {
                $user = $request->user();
                $tokenResult = $user->createToken('Personal Access Token');
                $token = $tokenResult->token;
                $token->save();

                $data = [
                    'user_id'      => $user->id,
                    'name'         => $user->name,
                    'email'        => $user->email,
                    'token_type'   => 'Bearer',
                    'access_token' => $tokenResult->accessToken,
                    'message'      => 'You are authenticated successfully.',
                    'status'       => 1
                ];
            }
        }

        $header = array(
            'Content-Type' => 'application/json; charset=UTF-8',
            'charset'      => 'utf-8'
        );

        return response()->json(['data' => $data], 200, $header, JSON_UNESCAPED_UNICODE);
    }

    /**
     * Register new user.
     *
     * @param Request $request
     * @return void
     */
    public function register(Request $request)
    {
        // Validate incoming request
        $validator = Validator::make($request->all(), [
            'name'     => 'required',
            'email'    => 'required|email|unique:users',
            'password' => 'required'
        ]);

        if ($validator->fails()) {
            $data = [
                'data'    => '',
                'message' => $validator->messages()->first(),
                'status'  => 0
            ];
        } else {
            $userObj = new $this->user([
                'name'         => $request->name,
                'email'        => $request->email,
                'password'     => Hash::make($request->password),
            ]);
            if ($userObj->save()) {
                $data = [
                    'user_id'      => $userObj->id,
                    'name'         => $userObj->name,
                    'email'        => $userObj->email,
                    'token_type'   => 'Bearer',
                    'access_token' => $userObj->createToken('Personal Access Token')->accessToken,
                    'message'      => 'You account created successfully.',
                    'status'       => 1
                ];
            } else {
                $data = [
                    'data'    => '',
                    'message' => 'Oops! Something went wrong.',
                    'status'  => 0
                ];
            }
        }

        $header = array(
            'Content-Type' => 'application/json; charset=UTF-8',
            'charset'      => 'utf-8'
        );

        return response()->json(['data' => $data], 200, $header, JSON_UNESCAPED_UNICODE);
    }

    /**
     * Logout user (Revoke the token).
     *
     * @param Request $request
     * @return void
     */
    public function logout(Request $request)
    {
        $request->user()->token()->revoke();

        $data = [
            'data' => '',
            'message' => 'You are successfully logged out.',
            'status' => 1
        ];

        return response()->json(['data' => $data]);
    }
}

MoviesController [app/Http/Controllers/API/MoviesControllers]

<?php

namespace App\Http\Controllers\API;

use App\Movies;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class MoviesController extends Controller
{
    protected $movies;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Movies $movies)
    {
        $this->movies = $movies;
    }

    /**
     * Get list of movies.
     *
     * @return void
     */
    public function getMovies()
    {
        $moviesObj = $this->movies->get();

        $data = [
            'data'    => $moviesObj,
            'message' => 'You are successfully logged out.',
            'status'  => 1
        ];

        return response()->json(['data' => $data]);
    }
}

Now open postman in your system and start palying with your API.

  • Registration (http://localhost/passport/public/api/auth/register)
  • Login (http://localhost/passport/public/api/auth/login)
  • List Of Movies (http://localhost/passport/public/api/auth/movies)
  • Logout (http://localhost/passport/public/api/auth/logout)

Screenshots:

That’s it. You have successfully build API using the Laravel Passport package in your Laravel application.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Laravel api integration & configuration

Recently I have worked on two Laravel mobile applications in my freelancing work and both are having android applications. So for the API authentication, I have used the Laravel Passport package. It’s very easy to authenticate each and every API using bearer token so I thought I have to create a new article on Laravel API integration & configuration which might be helpful for the new beginners who just started learning Laravel & Passport.

api integration

In this article, we are going to set up and configure Passport in fresh Laravel 6 application so please stick with the steps and later on we will build API with authentication in the next article.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Step 1:

Install the latest version of Laravel using the below commands.

composer create-project laravel/laravel passport

Step 2:

After installing Laravel go to your project directory and install laravel/ui package to setup all routes and views for the authentication purpose.

composer require laravel/ui --dev
 
php artisan ui vue --auth

npm install && npm run dev

Step 3:

Create a new database and set up a connection in your .env file.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=passport
DB_USERNAME=root
DB_PASSWORD=

Step 4:

Now install the Passport package in our Laravel application.

composer require laravel/passport

Once your installation completed, you have to set up a few things in your config/app.php file.

'providers' => [

    Laravel\Passport\PassportServiceProvider::class,
    
],

Few updates still required on below flies so please update accordingly.

  • app/User.php
  • app/Providers/AuthServiceProvider.php
  • config/auth.php

config/auth.php

<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

app/Providers/AuthServiceProvider.php

<?php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}

config/auth.php

...

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
        'hash' => false,
    ],
],

...

Step 4:

Generate tables using migration command.

php artisan migrate

If you are geeting error like “SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `u sers_email_unique`(`email`))” during migrate, refer this article: http://bit.ly/2JiCIVO

Step 5:

Generate passport token keys for the security.

php artisan passport:install --force

That’s it. You have successfully configured a Passport in your Laravel application.

Please visit my next blog post which is related to this article where you can learn on Laravel passport create API for your application.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Replace old to new url in the WordPress database

If you are new in WordPress development and working on a client project then one thing you have to take note is that whenever you deploy your WordPress project to client-server you have to replace your site URL from local to live and the same thing will apply, If you migrate from one to another domain. If you forgot to change URL then it will redirect to the old URL and that needs to take care of it.

Another way you can avoid this problem by creating a virtual host in your local server with a live domain.

replace old to new url in the wordpress database

So how to replace old to new URL in the WordPress database? Well, it’s very easy. You just have to replace your old URL with the new URL so follow the below steps.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Step 1:

If you have already uploaded the database to the server, then go to your phpMyAdmin and open your database.

Step 2:

Copy the below code and replace your old URL with http://www.oldurl.in and do the same for the new URL.

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl.in', 'https://www.mananpatel.in') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl.in','https://www.mananpatel.in');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl.in', 'https://www.mananpatel.in');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl.in','https://www.mananpatel.in');

After replacing your old & new URL, go to the SQL section in your phpMyAdmin and past your SQL queries and hit the Go button.

After hitting the Go button, SQL queries will replace all instances of old URLs to new URLs. For example, If your current WordPress address is http://www.oldurl.in, it will be changed to https://www.mananpatel.in.

IMPORTANT! Sometimes table prefix can be different than wp_. If you get error messages saying that tables wp_optionswp_postswp_posts and wp_postmeta cannot be found, check table prefix and update SQL query code. You can see table prefix on the left side of phpMyAdmin, under your WordPress database:

Step 3:

If the query was executed successfully, then you will see a green success message with the number of records is affected.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

PostGIS installation in Ubuntu 18.04 LTS “Bionic Beaver”

In my recent work, I have completed one project in Laravel application and it was very similar to the food ordering application like Swiggy/Zomato. For the database, I had used PostgreSQL object-relational and installed PostGIS extension.

PostGIS is open source software program that adds support for geographic objects to the PostgreSQL object-relational database.

postgis installation

I heard about the PostgreSQL database but I never used PostGIS extension so it was a new experience for me. When I was developing API for the mobile application, I had to find nearby restaurants, hotels & cinemas based upon user current location(Latitude and Longitude). So I was searching on google for how to store records with latitude & longitude and how to make queries which return records from the database and after doing some research on google, I found that PostGIS extension which helps me in this scenario.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Before we jump to the PostGIS installation part, I am assuming that you have already installed the latest version of PostgreSQL in your system.

Step 1:

Before you can install PostGIS extension, update your list of available packages so the packages from the new repository are added to the list.

sudo apt-get update

Step 2:

Once your sources update, install the PostGIS extension.

sudo apt-get install postgis

Enter Y when prompted to install PostGIS along with its necessary dependencies. Now we can connect to PostgreSQL and integrate PostGIS.

Step 3:

Using the sudo command and switch to the Postgres user:

sudo -i -u postgres

Step 4:

Connect to the PostGISDemo database:

psql -d PostGISDemo

Step 5:

Enable the PostGIS extension on the database:

CREATE EXTENSION postgis;

Step 6:

Ok. Now let’s verify that everything worked correctly. Execute the following command in your terminal screen:

SELECT PostGIS_version();

You’ll get this output:

            postgis_version
---------------------------------------
2.4 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
(1 row)

Step 7:

We’re all set. Type the below command

\q

to exit the SQL session and return to your terminal prompt.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Maintenance mode for Laravel application

Today I am going to show you one artisan command which is a very helpful command. Well, I am talking about the Artisan::call(‘up’) or Artisan::call(‘down’) command. Using this command you can build your own maintenance mode functionality.

This command will help you when something went wrong to your site and you need to shut down your site for the little times for the troubleshooting.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

If you just want to test this feature then open your Laravel project in the terminal and run php artisan down and then open your project in the browser and see the reflection on the site.

maintenance mode

Recently I have integrated maintenance mode functionality in my Laravel project and I must tell you that it’s too easy to set up on any Laravel project without any troubles.

Let’s say I have two types of user roles:

  1. Super Admin
  2. Business Owner

And I want to access login route & super admin group routes even If maintenance mode is enabled just like WordPress.

maintenance mode

Now let’s jump to the code and follow the steps.

Step 1:

Create new custom middleware.

php artisan make:middleware CheckForMaintenanceMode

Step 2:

After creating middleware open CheckForMaintenanceMode.php file from app/Http/Middleware directory and replace it with below code.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;

class CheckForMaintenanceMode
{
    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function handle($request, Closure $next)
    {
        if ($this->app->isDownForMaintenance() && !$this->isBackendRequest($request)) {
            $data = json_decode(file_get_contents($this->app->storagePath() . '/framework/down'), true);

            throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']);
        }

        return $next($request);
    }

    private function isBackendRequest($request)
    {
        return ($request->is('login') or $request->is('admin/*') or $request->is('api/*'));
    }
}

Inside isBackendRequest function, you can define routes that you want to allow even If maintenance mode is enabled.

Step 3:

Now we have to include CheckForMaintenanceMode middleware inside Kerel.php file and for that navigate to app/Http directory.

Here you have to do two things:

1. Add below one line of code inside protected $middleware = [ … ]

'App\Http\Middleware\CheckForMaintenanceMode',

2. Remove CheckForMaintenanceMode::class code from protected $middleware = [ … ]

\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

Step 4:

To make your site Online/Offline, create a new form with drop-down selection(Online/Offline) somewhere in your admin panel and manage flag into the database (is_online 1/0). Whenever the admin user changes the site availability, run Artisan code based on the selection.

if ($request->get('is_online') == '1') {
    \Artisan::call('up');
} else {
    \Artisan::call('down');
}

That’s it. You have successfully created a maintenance mode functionality in your Laravel application.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Free SSL certificate for your shared hosting server

Recently I have got a free SSL certificate and install on my shared hosting server so today I am going to show you how to get your free SSL certificate and install on your shared hosting server so let follow these steps.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Step 1:

Get your free SSL certificate from SSL For Free and enter your domain name.

free ssl certificate

Step 2:

Now select “Manual Verification (Upload verification files manually to your domain to verify ownership)” and click on the Manual verification button.

free ssl certificate

Step 3:

Download file #1 & #2 in your system and log in to your cPanel.

  1. Create a folder in your domain named “.well-known” if it does not already exist.
  2. Create another folder in your domain under “.well-known” named “acme-challenge” if it does not already exist
  3. Upload the downloaded files to the “acme-challenge” folder.
  4. Verify successful upload by visiting the following links in your browser.
  5. If the files do not show random alphanumeric characters or show an error then recheck that you are uploading in the correct place. Also, try viewing the page source (Right-click then click “view page source”) of the above links to make sure nothing else shows up but the verification file contents. If you use IIS then you may have to change your server config so that files without an extension (or the wildcard MIME type) serve as text/plain. Contact your host if you are unsure.
  6. Click Download SSL Certificate below.

free ssl certificate

Step 4:

After clicking on the Download SSL Certificate button you will get Certificate, Private Key & CA Bundle so saved these details.

free ssl certificate

Step 5:

Now go to SSL/TLS option in your cPanel and click on Manage SSL sites.

Step 6:

Select your Domain from drop-down option & fill up Certificate, Private Key & Certificate Authority Bundle. Now click on the Install Certificate button to install SSL on your selected Domain.

Congratulation you have successfully installed an SSL certificate for your domain. Now access your site with https.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Managing Cronjobs with Laravel

Laravel provides a feature to run specific jobs through only one cron on the server.  It automatically manages all the jobs through the artisan command you don’t need to set multiple jobs on the server to execute in the specific time and specific interval.

We can manage a task on the server that executes scripts which helps in sending daily/weekly reports to email id or update records in the database from our laravel application.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

The main use of cron job is in updating the databases, sending the emails, generate reports, executing the time-consuming tasks etc. We can also delete or create files from the server with the help of Cron Job.

In the Unix based machines, there is a configuration file called crontable which is also known as Crontab. This file system manages cron jobs for that particular user in which it is placed.

To set a cron job with the script, there are various steps you have to follow.

Step1:

Create A New Command Class

Firstly, we will generate a custom command by running the following PHP artisan command in the terminal which will generate a class file in the app/Console/Commands/ directory of your laravel application.

php artisan make:console CustomCommand

After running this command you will get a message ‘Console command created successfully in your terminal and you will see there is an auto-generated class file at app/Console/Commands/CustomCommand.php with default signature. You can also assign the terminal command name by using –command option as under.

php artisan make:console CustomCommand --command=custom:command

Step 2:  

Write a script for the task

Whenever the above command will be executed in your terminal It creates a class with handle method which is called when the task is scheduled to execute. Here I am taking a sample script to delete all inactive users from the database in the handle method.

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

class CustomCommand extends Command {

    /**
    * The name and signature of the console command.
    * @var string
    */

    protected $signature = 'custom:command';

    /**
    * The console command description.
    * @var string
    */

    protected $description = 'Delete all inactive users';

    /**
    * Create a new command instance.
    * @return void
    */

    public function __construct()
    {
        parent::__construct();
    }

    /**
    * Execute the console command.
    * @return mixed
    */

    public function handle()
    {
        DB::table('users')->where('active', 0)->delete();
        $this->info('All inactive users are deleted successfully!');
    }
}

Now we have to register our new command with Artisan so that it will be available in the terminal. For this, we just need to add this command class name to commands array in the Kernel class that is available in app/Console/Kernel.php.

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{

    /**
    * The Artisan commands provided by your application.
    * @var array
    */

    protected $commands = [
        Commands\CustomCommand::class,
    ];

    /**
    * Define the application's command schedule.
    * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
    * @return void
    */

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('custom:command')->daily();
    }
}

If you want to see your command description in terminal then run following command:

php artisan list

Now you can run your command to delete all inactive users.

php artisan custom:command

All inactive users are deleted successfully!

As you notice I schedule the command on a daily basis but there is a number of schedule frequencies that you can assign to the tasks.

The schedule tasks hourly basis

$schedule->command('custom:command')->hourly();

If you want to start the scheduler itself then you will have to add one cronjob on the server using the crontab -e command.

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

Using this, the Laravel command scheduler will be called by cron every minute.

Happy Coding 😉

How to Remove the WordPress Version Number

By default WordPress, the version number is enabled and show in the code of WordPress site installation. Generally, there is no need to remove the version number.

Maybe someone doesn’t want to reveal the version number of the WP site. It is generally needed when you don’t update WordPress when the update available.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Using the below code version number from WordPress front-end code can be removed. Add this code in functions.php of your active theme.

// Remove WordPress Version number
function remove_wordpress_version_number() {
    return '';
}
add_filter('the_generator', 'remove_wordpress_version_number');

Happy Coding 😉

Solving WordPress Asking for FTP Credentials When Installing Plugins or Themes

Most web hosting is configured to automate installing themes or plugins. On some hosting WordPress ask for FTP details either to update or install a theme or Plugin.

To fix this issue we can enter our FTP credentials in the wp-config.php file so that we don’t have to enter it every time we update a plugin.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

It also helps in the new automatic update feature introduced by WordPress.

Enter the following code in the wp-config.php file:

define('FTP_USER', 'username');
define('FTP_PASS', 'password');
define('FTP_HOST', 'ftp.mananpatel.in:21');

Where

  • username is ftp username.
  • password is ftp password.
  • ftp.mananpatel.in is ftp hostname and 21 is port number.

Alternatively, We can also override default file and directory permission by entering following lines in wp-config.php

define( 'FS_CHMOD_FILE', 0644 );
define( 'FS_CHMOD_DIR', 0755 );
Where
  • FS_CHMOD_FILE is file permission &
  • FS_CHMOD_DIR is directory permission.

Happy Coding 😉