Skip to content

Instantly share code, notes, and snippets.

@sam-ngu
sam-ngu / iterate.php
Last active June 24, 2021 11:54
PHP iterator to recursively iterate through a folder.
<?php
$folderPath = __DIR__ . '/path/to/folder/';
try {
$dirIterator = new \RecursiveDirectoryIterator($folderPath);
/** @var \RecursiveDirectoryIterator | \RecursiveIteratorIterator $it */
$it = new \RecursiveIteratorIterator($dirIterator);
// the valid() method checks if current position is valid eg there is a valid file or directory at the current position
@sam-ngu
sam-ngu / route_group_array_syntax.php
Last active June 16, 2021 08:08
Laravel Route group: array syntax vs method syntax. Blog article:
<?php
// using array syntax
Route::group([
'middleware' => [
'auth:api',
\App\Http\Middleware\RedirectIfAuthenticated::class,
],
'prefix' => 'heyaa', // adding url prefix to all routes in this group
'as' => 'users.', // adding route name prefix to all routes in this group
'namespace' => "\App\Http\Controllers",
@sam-ngu
sam-ngu / default_conda_env.sh
Created June 14, 2021 03:47
How to change default anaconda env in Unix based system.
# in ~/.bashrc
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/sam/anaconda3/bin/conda' shel$
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
@sam-ngu
sam-ngu / vue3-webcomponent-main.js
Created June 3, 2021 10:51
Sample code to create a Vue 3 web component.
import { createApp, h } from 'vue'; // import components from Vue 3
import App from './App.vue';
// importing vue 3 web component wrapper
// https://www.npmjs.com/package/vue3-webcomponent-wrapper
import wrapper from "vue3-webcomponent-wrapper";
// This line will mount our component to the dom. This is only used in local development.
// but it is not necessary for the webcomponent wrapper to work.
// You can comment this out
@sam-ngu
sam-ngu / deploy.sh
Created May 23, 2021 05:28
Bash script to deploy a React app to Github pages
#!/usr/bin/env sh
# abort on errors
set -e
# build
npm run build
# navigate into the build output directory
cd build
@sam-ngu
sam-ngu / formdata.js
Last active April 14, 2021 00:16
Form Data demo
// payload to send in the API request
const payload = {
email: "example@example.com",
recipients: [
'test1@example.com',
'test2@example.com',
],
attachment: fileObject,
}
@sam-ngu
sam-ngu / composer-install.sh
Created December 7, 2020 01:59
Install composer globally
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php composer-setup.php --install-dir=/usr/bin --filename=composer
php -r "unlink('composer-setup.php');"
@sam-ngu
sam-ngu / Composer.php
Created November 10, 2020 01:40
Laravel helper class to run composer command programmatically.
<?php
namespace App;
class Composer extends \Illuminate\Support\Composer
{
public function run(array $command)
{
@sam-ngu
sam-ngu / SitewideSearchController.php
Last active September 22, 2023 01:53
An implementation of full site search in Laravel
<?php
namespace App\Http\Controllers;
use App\Http\Resources\SiteSearchResource;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
@sam-ngu
sam-ngu / parse_url_parameter_name.php
Created August 24, 2020 13:02
Parsing URL parameter name using Symfony Route.
<?php
$uri = 'https://test.com/api/users/{user}?page=4&size=5';
$route = new SymfonyRoute(
preg_replace('/\{(\w+?)\?\}/', '{$1}', $uri), [], [], ['utf8' => true, 'action' => []],
'', [], []
);
$compiled = $route->compile();