Extending


Custom directory

/var/www/[instance]/app/laravel/custom

Repository

https://bitbucket.org/cstore-team/customs/src/main

Each modified instance should contain a separate branch (from main branch).

Extending classes

You can easy extend any controllers, models, and main classes.

To extend a class, it must be in the "Base" directory. Some classes have not yet been adapted to extending. If you need to extend such a class, you must adjust class in the main repository.

Controllers

Place a file with the same name in the directory custom/app/Http/Controllers/Extended.

Example

custom/app/Http/Controllers/Extended/ProductsController.php

<?php

namespace Custom\App\Http\Controllers\Extended;

use App\Models\Product;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class ProductsController extends \App\Http\Controllers\Base\ProductsController
{
    // You can add here a new method, or overwrite existing.

    // The following example overrides the product name in the product view.

    public function product(Request $request, string $name, Product $product): View|RedirectResponse
    {
        $product->setLanguageField('name', 'Overwritten product name');

        return parent::product($request, $name, $product);
    }
}

Models

Place a file with the same name in the directory custom/app/Models/Extended.

Example

custom/app/Models/Extended/Product.php

<?php

namespace Custom\App\Models\Extended;

use App\Models\User;
use App\Price;

class Product extends \App\Models\Base\Product
{
    // We enforce a 1 PLN price for each product. This change is visible e.g. in the product view.

    public function getPrice(bool $formatted = false, bool $ignorePromotionalPrice = false, string $type = null, bool $repository = false, float $quantity = 1, User $user = null): float|string|null
    {
        return Price::format(1, formatted: $formatted);
    }
}

Extending views

Place a file with the same name under similar directory structure in the directory custom/resources/template.

Example

custom/resources/template/article.blade.php

<!-- You can overwrite the entire view or modify part of it. -->

<!-- This example inherits the article view and adds a new section. Visible e.g. on the home page (home page is also an article). -->

@push('before-container')
    <h2 class="text-center text-red">{{ __('Custom content') }}</h2>
@endpush

@includeDefault('article')

Adding routes

You can add custom routes in the custom/routes/custom.php file.

Custom migration

custom/database/migrations/custom_migrations.php

Custom service provider

custom/app/Providers/CustomServiceProvider.php

Config

@todo

Importers / Exporters

Your data exchange scripts should be inside the directory custom/app/Console/Commands.

You can use existing files or create new ones.

Type php artisan custom:data:import to run CustomImporter.php script.

Type php artisan custom:data:export to run CustomExporter.php script.

You can manage the available scripts from admin panel.

https://[instance].l2.cloud.cstore.pl/admin/importers

https://[instance].l2.cloud.cstore.pl/admin/exporters

Global commands

php artisan data:import

php artisan data:export

Import

There are two basic classes that you can extend. First when you import products, second for other data.

app/Sync/Importer.php

app/Sync/CompleteImporter.php

Export

You can extend app/Sync/Exporter.php class when you export data.

Useful classes

  • app/Sync/CSV.php
  • app/Sync/XML.php

Documentation

To create documentation for a custom project, simply create a new custom.md file in the custom/resources/docs/1.0/ directory. If this directory doesn't exist, create it. Additionally, you have the flexibility to override any existing .md file in the docs directory, which will replace the original file.

Storage

To share files from the custom directory, place them in the custom/storage folder.

These files will be available from https://[instance].l2.cloud.cstore.pl/custom url.