PHP code example of teksite / extralaravel

1. Go to this page and download the library: Download teksite/extralaravel 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/ */

    

teksite / extralaravel example snippets


return [
    // ...
    Teksite\Extralaravel\ExtraLaravelServiceProvider::class,
];

return [

    // Hidden field used by the honeypot middleware and the @honeypot directive.
    'honeypot' => [
        'enabled'    => true,
        'status'     => 403, // must be a 4xx/5xx code, otherwise 403 is used
        'field_name' => env('HONEYPOT_FIELD_NAME', 'honeypot'),
    ],

    // Application locales in which JalaliDateCast converts between Gregorian and Jalali dates.
    'jalali_date_locales' => ['fa'],

    // Locales treated as right-to-left by is_rtl().
    'rtl_locales' => ['ar', 'fa', 'he', 'ur', 'ps', 'ku', 'ckb', 'sd', 'yi', 'dv', 'ug'],

    // Length limits used by PasswordRule when no explicit values are passed to its constructor.
    'password' => [
        'min_length' => 8,
        'max_length' => 128,
    ],

    // Directory (relative to base_path()) that getStaticData() may read PHP files from.
    'data_path' => 'app/data',
];

use Teksite\Extralaravel\Casts\IpCast;

protected $casts = [
    'ip' => IpCast::class,
];

$model->ip = null;            // stores the current request IP
$model->ip = '192.168.1.10';  // stored as given
$model->ip = 'not-an-ip';     // throws InvalidArgumentException

use Teksite\Extralaravel\Casts\JalaliDateCast;

protected $casts = [
    'published_at' => JalaliDateCast::class,
];

app()->setLocale('fa');

$post->published_at;                       // "1403/05/10 14:30:00"  (date-only columns: "1403/05/10")

$post->published_at = '1403/05/10 14:30';  // saved as "2024-07-31 14:30:00"
$post->published_at = '۱۴۰۳/۰۵/۱۰';        // Persian digits are accepted, saved as "2024-07-31"

use Teksite\Extralaravel\Casts\SlugCast;

protected $casts = [
    'slug' => SlugCast::class,
];

$post->slug = 'My Awesome Post';   // "my-awesome-post"
$post->slug = 'MyPost';            // "my-post"   (camelCase is split)
$post->slug = 'HTMLParser';        // "html-parser"
$post->slug = null;                // null
$post->slug = 123;                 // throws InvalidArgumentException (must be a string)

use Teksite\Extralaravel\Enums\Areas;

Areas::tryFrom('Iran');                       // Areas::IR
collect(Areas::cases())->pluck('value', 'name'); // ['IR' => 'Iran', ...] for a select box

use Teksite\Extralaravel\Enums\TimeZones;

TimeZones::allZones();                         // ['Asia/Tehran', ...]
TimeZones::allTimes();                         // unique offsets, e.g. ['+3:30', ...]

TimeZones::getOffset('Asia/Tehran');           // "+3:30"   (standard offset, never changes during the year)
TimeZones::getOffset(TimeZones::Asia_Tehran);  // "+3:30"
TimeZones::getOffset('Mars/Base');             // null

// Daylight-saving aware, computed from PHP's timezone database:
TimeZones::getCurrentOffset('America/Los_Angeles');                          // "-7:00" in summer, "-8:00" in winter
TimeZones::getCurrentOffset('America/Los_Angeles', new DateTimeImmutable('2026-01-15')); // "-8:00"

use Teksite\Extralaravel\Enums\MobilePatterns;

MobilePatterns::IRAN->validate('۰۹۱۲ ۳۴۵ ۶۷۸۹');   // true
MobilePatterns::IRAN->validate('+98 912 345 6789'); // true
MobilePatterns::IRAN->validate('abc');              // false

MobilePatterns::country('uk');                         // MobilePatterns::UNITED_KINGDOM
MobilePatterns::fromDialCode('+1');                    // [USA, CANADA]
MobilePatterns::detectCountry('+447400123456');        // MobilePatterns::UNITED_KINGDOM
MobilePatterns::detectCountries('+12015550123');       // [USA, CANADA]

MobilePatterns::canonical('09123456789', 'iran');      // "989123456789"
MobilePatterns::e164('09123456789', 'iran');           // "+989123456789"
MobilePatterns::areSame('0912 345 6789', '+98 912 345 6789', 'iran'); // true

MobilePatterns::mask('+989123456789');                 // "+989******789"
MobilePatterns::options(sortByLabel: true);            // ['AF' => 'Afghanistan', ...]
MobilePatterns::IRAN->example(true);                   // "+989123456789"
MobilePatterns::IRAN->flag();                          // "🇮🇷"

use Teksite\Extralaravel\Rules\CodeMeliRule;

$request->validate([
    'national_code' => ['

use Teksite\Extralaravel\Enums\MobilePatterns;
use Teksite\Extralaravel\Rules\MobileRule;

new MobileRule();                                             // Iran
new MobileRule('germany');                                    // by name
new MobileRule('DE');                                         // by ISO code
new MobileRule(MobilePatterns::TURKEY);                       // by enum
new MobileRule(['iran', MobilePatterns::USA, 'iraq']);        // any of several countries

use Teksite\Extralaravel\Rules\PasswordRule;

new PasswordRule();          // limits from config: 8 to 128 characters
new PasswordRule(10);        // minimum 10
new PasswordRule(10, 64);    // minimum 10, maximum 64

use Teksite\Extralaravel\Rules\NoHtmlRule;

'name' => ['

use Teksite\Extralaravel\Rules\NeverPassRule;

'field' => [new NeverPassRule()],

namespace App\Http\Requests;

use Teksite\Extralaravel\Http\ApiFormRequest;

class StoreUserRequest extends ApiFormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'name'  => ['

use Illuminate\Foundation\Auth\User as Authenticatable;
use Teksite\Extralaravel\Traits\MustVerifyPhone;

class User extends Authenticatable
{
    use MustVerifyPhone;

    protected function casts(): array
    {
        return ['phone_verified_at' => 'datetime'];
    }

    // Required: the package does not depend on any SMS provider.
    public function sendPhoneVerificationNotification(): void
    {
        // Send an SMS or notification here.
    }
}

use App\Models\User;
use Teksite\Extralaravel\Traits\TrashMethods;

class UserTrashManager
{
    use TrashMethods;

    protected function getModelClass(): string
    {
        return User::class;
    }
}

$manager = new UserTrashManager();

$manager->getTrashes(perPage: 50);
$manager->getTrashes(perPage: 20, columns: ['id', 'name'], page: 2);

$manager->restoreOne(10);
$manager->restore([10, 20, 30]);
$manager->wipeAllWithEvents();

protected function trashChunkSize(): int { return 500; }     // default 1000
protected function trashMaxPerPage(): int { return 100; }    // default 250
protected function trashedQuery(): Builder { /* add scopes, e.g. tenant filters */ }

use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::trashResource('users', UserController::class);

Route::trashResource('users.posts', PostController::class);
// GET    users/{user}/posts/trash
// PATCH  users/{user}/posts/trash/{post}   (name: users.posts.trash.reinstate)

Route::trashResource('users', UserController::class, [
    'prefix'     => 'admin',
    'middleware' => ['auth', 'can:manage-trash'],
    'as'         => 'admin',            // route names become admin.users.trash.index, ...
    'only'       => ['index', 'restore'],
]);

Route::trashResource('users', UserController::class)
    ->prefix('admin')
    ->middleware(['auth', 'can:manage-trash'])
    ->as('admin')
    ->except(['flush']);

// routes/console.php
Schedule::command('cache:purge --force')->daily();

   Route::post('/contact', ContactController::class)->middleware('honeypot');
   

$items = ['a', ['b', ['c', 'd']], 'e'];
// renders a, b, c, d, e

changeToSlug('My Awesome Post');          // "my-awesome-post"
changeToSlug('My Awesome Post', '_');     // "my_awesome_post"
toEnglishNumber('۱۲۳۴۵');                 // "12345"
convertSeconds(3661);                     // "01:01:01"
convertSeconds(3661, 'array');            // ['hours' => 1, 'minutes' => 1, 'seconds' => 1]
exploding('one,two،three')->all();        // ['one', 'two', 'three']
arrayToDot('users[0][name]');             // "users.0.name"
dotToArray('users.0.name');               // "users[0][name]"
is_rtl('fa_IR');                          // true
get_error($errors, 'users[0][email]');    // $errors->get('users.0.email')

// app/data/times/iran.php
return [
    'tehran' => ['timezone' => 'Asia/Tehran'],
];

getStaticData('app.data.times.iran.tehran.timezone');            // "Asia/Tehran"
getStaticData('app.data.unknown', 'fallback');                   // "fallback"
getStaticData('times.iran.tehran.timezone', 'x', 'app.data');    // "Asia/Tehran" (with $prePath)
bash
php artisan vendor:publish --tag=extralaravel-config
bash
php artisan make:request-api StoreUserRequest
php artisan make:request-api Admin/StoreUserRequest
bash
php artisan make:logic UserLogic
php artisan make:logic User/UserLogic
bash
php artisan make:service BillingService
bash
php artisan vendor:publish --tag=extralaravel-stubs