# Add Uccello to a Laravel project

It is possible to add Uccello to an existing Laravel project (version >= 5.6).

{% hint style="danger" %}
Since Uccello has its own authentication interface, remember to backup the following files and readjust them if necessary:

* resources/views/auth/login.blade.php
* resources/views/auth/register.blade.php
* resources/views/auth/passwords/email.blade.php
* resources/views/auth/passwords/reset.blade.php
  {% endhint %}

### 1. Install the Uccello library

You can install the `uccello/uccello` dependency with composer.

```bash
composer require uccello/uccello
```

If you are using a version of Laravel **lower than 5.5**, you will need to add the following code in the `config/app.php` file:

{% tabs %}
{% tab title="config/app.php" %}

```php
'providers' => [
    ...
    /*
     * Package Service Providers...
     */
    Uccello\Core\Providers\AppServiceProvider::class,
    Uccello\Core\Providers\RouteServiceProvider::class,
    ...
],
...
'aliases' => [
    ...
    'Uccello' => Uccello\Core\Facades\Uccello::class,
],
```

{% endtab %}
{% endtabs %}

Then execute the following command to extract all the files necessary for Uccello to work properly:

{% hint style="warning" %}
For Laravel >= 6.\*, it is necessary to install laravel/ui for adding auth management:

Laravel 6.x :`composer require laravel/ui:1.*`

Laravel 7.x :`composer require laravel/ui:2.*`

Laravel 8.x :`composer require laravel/ui:3.*`
{% endhint %}

```bash
php artisan make:auth # For Laravel < 6.x
php artisan ui:auth # For Laravel >= 6.x
php artisan uccello:install
```

### 2. Add the middlewares

Open the file `app/Http/Kernel.php` and add the following code to add the [middlewares](https://laravel.com/docs/5.8/middleware) used by Uccello :

{% tabs %}
{% tab title="app/Http/Kernel.php" %}

```php
protected $routeMiddleware = [
  ...
  'uccello.permissions' => \Uccello\Core\Http\Middleware\CheckPermissions::class,
  'uccello.settings' => \Uccello\Core\Http\Middleware\CheckSettingsPanel::class,
];
```

{% endtab %}
{% endtabs %}

### 3. Configure the routes

Add the following code to the `routes/web.php` file to automatically generate the route for to access Uccello.

```php
...
Route::get('/uccello', function() {
    $domain = uccello()->useMultiDomains() ? uccello()->getLastOrDefaultDomain()->slug : null;
    $route = ucroute('uccello.home', $domain);
    return redirect($route);
});
```

This automatically detects whether the notion of **multi domains** is used or not and redirects the user to the **last domain** he visited.

{% hint style="info" %}
If you want to access Uccello from your home page, just replace `Route::get('/uccello', ...)` by  `Route::get('/', ...)`
{% endhint %}

### 4. Configure the environment

Once the project is created you can configure the `.env` file as explained in Laravel's [official documentation](https://laravel.com/docs/5.7/database#configuration).

{% tabs %}
{% tab title=".env" %}

```
APP_URL=http://localhost:8000
...
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=uccello
DB_USERNAME=homestead
DB_PASSWORD=secret
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
This is an example. Put your own data into the `.env` file.
{% endhint %}

If you do not want to use the notion of multi domains, you can add the following line into the `.env` file:

{% tabs %}
{% tab title=".env" %}

```
...
UCCELLO_MULTI_DOMAINS=false
```

{% endtab %}
{% endtabs %}

### 5. Execute migrations

Once the database is configured, it is now possible to execute [migrations](https://laravel.com/docs/5.7/migrations) to create the database structure used by Uccello.

```bash
php artisan migrate
```

### 6. Create an user

To easily create a new user, execute the following comman&#x64;**:**

```bash
php artisan uccello:user
```

### 7. Activate connection log

You can see last connections of a user in his detail page. To activate this functionality edit the file located at `app/Http/Controllers/Auth/LoginController.php` and the following code:

```php
use Carbon\Carbon;
use Uccello\Core\Models\Connection;

class LoginController extends Controller
{
  // ...
  
  /**
    * The user has been authenticated.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  mixed  $user
    * @return mixed
    */
    protected function authenticated(Request $request, $user)
    {
        Connection::create([
            'user_id' => $user->id,
            'datetime' => Carbon::now(),
        ]);
    }
}
```

### 8. It's ready!

If you would like to use PHP's built-in development server to serve your application, this command will start a local development server:

```bash
php artisan serve
```

Now you can go to the home page of your site: <http://localhost:8000>. Once redirected to the login page, you can authenticate yourself with the credentials created at the step 6.
