Overriding

Uccello was designed to make it very easy for developers to create applications that allow them to manage their data. However, it is quite possible to customize Uccello's behavior to meet specific needs.

For this purpose it is possible to override routes, controllers, views and translations.

Override a View

It is extremely easy to override a view. If you create a blade file using the kernel directory architecture, Uccello will automatically detect your file and use it first. You can do this for all modules or for a particular module.

To override a view for a particular module, use this path: resources/views/uccello/modules/moduleName/viewName/main.blade.php

To override a view for all modules, use this path:

resources/views/uccello/modules/default/viewName/main.blade.php

Here is the list of all default views name:

  • index

  • list

  • detail

  • edit

It is only possible to override main.blade.php files. If you want to override other files, you can copy them from the kernel and adapt them in your application.

Example 1

If you want to replace the home page, create the file resources/views/uccello/modules/home/index/main.blade.php with the following content:

@extends('uccello::modules.home.index.main')

@section('content')
<div class="row">
    <div class="col s12">
        This is my overrided home page!
    </div>
</div>
@endsection

Example 2

If you want to replace all index pages, create the file resources/views/uccello/modules/default/index/main.blade.php with the following content:

@extends('uccello::modules.default.index.main')

@section('content')
<div class="row">
    <div class="col s12">
        This is my overrided index page!
    </div>
</div>
@endsection

The overriding of a view from a specific module has priority over the overriding of a default view.

Priority

To detect which view to use, Uccello uses the following priorities:

  1. Module view overridden in app

  2. Default view overridden in app

  3. Module view overridden in package

  4. Default view defined in package

  5. Module view overridden in uccello

  6. Default view defined in uccello

  7. Fallback view if defined

Override an Uitype

As with views, it is very easy to override a uitype.

Each uitype can have its own HTML representation in the following views:

  • Search field of the list view

  • Cell of the table in the list view table

  • Detail view

  • Edit view

You can view the list of default uitypes views, on the Uccello Git repository or directly from your application in the following directory:vendor/uccello/uccello/resources/views/modules/default/uitypes

It is possible to override a uitype on a module level or for the entire application.

To override a uitype's view for a particular module, use this path: resources/views/uccello/modules/moduleName/uitypes/viewName/uitypeName.blade.php

To override a view for all modules, use this path:

resources/views/uccello/modules/default/uitypes/viewName/uitypeName.blade.php

Here is the list of all default uitypes views name:

  • search

  • list

  • detail

  • edit

If no views are defined for a specific uitype, Uccello uses by default the views defined for the text uitype.

For example, in a detail view, Uccello uses the file resources/views/modules/default/uitypes/detail/text.blade.php to represent the integeruitype.

Override a Controller

For override a Controller, you have to override 2 things:

  • The controller of your choice

  • The road to access this controller

Controller

First of all you have to create a new controller which will extends the original one. For example you want to override the DetailController for the user module.

Create a file located at app/Http/Controllers/User/DetailController.php and add the following code:

<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use Uccello\Core\Http\Controllers\Core\DetailController as UccelloDetailController;
use Uccello\Core\Models\Domain;
use Uccello\Core\Models\Module;

class DetailController extends UccelloDetailController
{
    /**
     * {@inheritdoc}
     */
    public function process(?Domain $domain, Module $module, Request $request)
    {
        // Get default view
        $view = parent::process($domain, $module, $request);

        // Useful if multi domains is not used
        $domain = $this->domain;

        // Get record
        $record = $this->getRecordFromRequest();

        // Your code here....

        // Add data to the view
        $view->myVariable = 'myValue';

        return $view;
    }
}

Route

Modify the routes file located at routes/web.php and add the following code for overriding the route that points to the Detail View of the user module.

<?php

...

// Adapt params if we use or not multi domains
if (!uccello()->useMultiDomains()) {
    $domainParam = '';
    $domainAndModuleParams = '{module}';
} else {
    $domainParam = '{domain}';
    $domainAndModuleParams = '{domain}/{module}';
}

Route::get($domainParam.'/user/detail', 'User\DetailController@process')
    ->defaults('module', 'user')
    ->middleware('uccello.permissions:retrieve')
    ->name('user.detail');

Now the route points to your overridden Controller.

Override a Model

To override a Model you have to override 2 things:

  • The model of your choice

  • The module's model_class attribute in the database

Model

Simply create a new model extending the original one, and put your overriding modification inside.

<?php

namespace App;

use Uccello\Crm\Models\Role as UccelloRole;

class Role extends UccelloRole
{
    // Put your overriding modification here
}

Attribute

Create a migration file and modify the uccello_modules table and change model_class attribute for using your new model.

<?php

use Uccello\Crm\Models\Module;

// Inside your migration file:
$module = Module::where('name', 'role')->first();
$module->model_class = 'App\Role';
$module->save();

Override a Localization file

It is very simple to override a Localization file from another package. To do this, just create a file located at resources/lang/{lang_code}/{module_name}.php and create a key for each translation label you want to override.

Example

To override the domain module translation file in english, just create a file located at resources/lang/en/domain.php and add the following code:

<?php

return [
    'domain' => 'Companies',
    'field' => [
        'name' => 'Company\'s name'
    ]
];

Now the domain module will be named Companies and the name field will be named Company's name.

Last updated