PHP code example of bambolee-digital / translatable-resource-kit

1. Go to this page and download the library: Download bambolee-digital/translatable-resource-kit 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/ */

    

bambolee-digital / translatable-resource-kit example snippets


return [
    // disable debug 
    'debug' => false,
    // Disable automatic middleware registration
    'disable_middleware' => false,
    // Specify the middleware group (default is 'api')
    'middleware_group' => 'api',
    // ... other configurations
];

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\API\ProductController;
use App\Http\Controllers\API\CategoryController;

Route::middleware(['set_locale', SetLocale::class])->group(function () {
    // Product routes
    Route::prefix('products')->group(function () {
        Route::get('/', [ProductController::class, 'index']);
        Route::get('/{product}', [ProductController::class, 'show']);
        Route::post('/', [ProductController::class, 'store']);
        Route::put('/{product}', [ProductController::class, 'update']);
        Route::delete('/{product}', [ProductController::class, 'destroy']);
    });

    // Category routes
    Route::prefix('categories')->group(function () {
        Route::get('/', [CategoryController::class, 'index']);
        Route::get('/{category}', [CategoryController::class, 'show']);
        Route::post('/', [CategoryController::class, 'store']);
        Route::put('/{category}', [CategoryController::class, 'update']);
        Route::delete('/{category}', [CategoryController::class, 'destroy']);
    });

    // Add more route groups as needed
});

use BamboleeDigital\TranslatableResourceKit\Http\Traits\TranslatesAttributes;
use Spatie\Translatable\HasTranslations;

class Product extends Model
{
    use HasTranslations, TranslatesAttributes;

    public $translatable = ['name', 'description'];

    // ...
}

use BamboleeDigital\TranslatableResourceKit\Http\Resources\TranslatableResource;

class ProductResource extends TranslatableResource
{
    // You can add custom logic here if needed
}

use BamboleeDigital\TranslatableResourceKit\Http\Resources\TranslatableCollection;

class ProductCollection extends TranslatableCollection
{
    // You can add custom logic here if needed
}

public function index()
{
    $products = Product::all();
    return new ProductCollection($products);
}

public function show(Product $product)
{
    return new ProductResource($product);
}

use Illuminate\Support\Facades\Route;
use BamboleeDigital\TranslatableResourceKit\Http\Middleware\SetLocale;

public function boot()
{
    Route::aliasMiddleware('set_locale', SetLocale::class);
    Route::pushMiddlewareToGroup('api', 'set_locale');
}

use BamboleeDigital\TranslatableResourceKit\Http\Middleware\SetLocale;

Route::middleware([SetLocale::class])->group(function () {
    // Your API routes here
});

protected $middlewareGroups = [
    'api' => [
        // ...
        \BamboleeDigital\TranslatableResourceKit\Middleware\SetLocale::class,
    ],
];

return [
    'max_recursion_depth' => 3, // Default is 5
];

class Product extends Model
{
    use HasTranslations, TranslatesAttributes;

    protected $with = ['category', 'tags'];

    // ...
}
bash
php artisan vendor:publish --provider="BamboleeDigital\TranslatableResourceKit\TranslatableResourceKitServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="BamboleeDigital\TranslatableResourceKit\TranslatableResourceKitServiceProvider" --tag="middleware"