PHP code example of somarkn99 / apibasicsetting

1. Go to this page and download the library: Download somarkn99/apibasicsetting library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

somarkn99 / apibasicsetting example snippets


composer 

class AcceptJsonResponse
{
    public function handle($request, Closure $next)
    {
        $request->headers->set('Accept', 'application/json');

        return $next($request);
    }
}

class CORS
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->headers->set('Access-Control-Allow-Origin', '*');
        $response->headers->set('Access-Control-Allow-Methods', '*');
        $response->headers->set('Access-Control-Allow-Credentials', true);
        $response->headers->set('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,X-Token-Auth,Authorization');

        return $response;
    }
}

class FingerPrintHeader
{
    public function handle($request, Closure $next)
    {
        $request->headers->remove('X-Powered-By');
        $request->headers->remove('Server');

        return $next($request);
    }
}

class Host
{
    public function handle($request, Closure $next)
    {
        $RequestHost = parse_url(\Illuminate\Support\Facades\URL::full())['host'];
        $AcceptedHost = explode(',', env('ACCEPTED_HOST'));

        if (in_array($RequestHost, $AcceptedHost) == true || $RequestHost == 'localhost') {
            return $next($request);
        } else {
            abort(403);
        }
    }
}

class localization
{
    public function handle($request, Closure $next)
    {
        // Check header request and determine localization
        $local = ($request->hasHeader('X-localization')) ? $request->header('X-localization') : 'en';

        // set laravel localization
        app()->setLocale($local);

        // continue request
        return $next($request);
    }
}

class SecureCheck
{
    public function handle(Request $request, Closure $next)
    {
        if (! $request->secure() && App::environment('production')) {
            return response()->json("Please use https protocol so you can send requests.", Response::HTTP_BAD_REQUEST);
        }

        return $next($request);
    }
}

    function _dd(...$args)
    {
        $trace = debug_backtrace();
        $path = '';
        $path .= isset($trace[1]['class']) ? class_basename($trace[1]['class']) : '';
        $path .= isset($trace[1]['function']) ? '@'.$trace[1]['function'].'()' : '';
        $path .= isset($trace[1]['function']) ? ' => line('.$trace[0]['line'].')' : null;

        return response()->json([
            'Path' => $path,
            'dd_Data' => $args,
        ],Response::HTTP_INTERNAL_SERVER_ERROR);
        exit();
    }

    function setEnv($key, $value)
    {
        $path = base_path('.env');
        if (file_exists($path)) {
            file_put_contents($path,
                str_replace($key.'='.env($key), $key.'='.$value,
                    str_replace($key.'="'.env($key).'"', $key.'="'.$value.'"',
                        file_get_contents($path))
                ));
        }
    }

    function checkIfFileExists($file, $name)
    {
        if (isset(request()->all()[$name])) {
            if (gettype(request()->all()[$name]) !== 'array') {
                if (! isset($file) || is_null($file) || ! request()->hasFile($name)) {
                    return response()->json('please make sure you store correct file.', Response::HTTP_BAD_REQUEST);
                }
            }
        }
    }

function dateFormat($date)
{
    return \Carbon\Carbon::parse($date)->toFormattedDateString();
}