PHPackages                             tobento/app-country - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tobento/app-country

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tobento/app-country
===================

App country support.

2.0(7mo ago)090↓33.3%1MITPHPPHP &gt;=8.4

Since Jul 13Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/app-country)[ Packagist](https://packagist.org/packages/tobento/app-country)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-app-country/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (2)Dependencies (8)Versions (4)Used By (1)

App Country
===========

[](#app-country)

Country support for the app.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting Started](#getting-started)
    - [Requirements](#requirements)
- [Documentation](#documentation)
    - [App](#app)
    - [Country Boot](#country-boot)
        - [Available Country Interfaces](#available-country-interfaces)
        - [Retrieve Countries](#retrieve-countries)
        - [Add Or Customize Countries](#add-or-customize-countries)
    - [Learn More](#learn-more)
        - [Using Countries In Forms](#using-countries-in-forms)
    - [Country Groups](#country-groups)
- [Credits](#credits)

---

Getting Started
===============

[](#getting-started)

Add the latest version of the app country project running this command.

```
composer require tobento/app-country

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Documentation
=============

[](#documentation)

App
---

[](#app)

Check out the [**App Skeleton**](https://github.com/tobento-ch/app-skeleton) if you are using the skeleton.

You may also check out the [**App**](https://github.com/tobento-ch/app) to learn more about the app in general.

Country Boot
------------

[](#country-boot)

The country boot does the following:

- installs country files
- implements country interfaces

```
use Tobento\App\AppFactory;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);

// Run the app
$app->run();
```

### Available Country Interfaces

[](#available-country-interfaces)

The following interfaces are available after booting:

```
use Tobento\App\AppFactory;
use Tobento\Service\Country\CountryFactoryInterface;
use Tobento\Service\Country\CountriesFactoryInterface;
use Tobento\Service\Country\CountryRepositoryInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);
$app->booting();

$countryFactory = $app->get(CountryFactoryInterface::class);
$countriesFactory = $app->get(CountriesFactoryInterface::class);
$countryRepository = $app->get(CountryRepositoryInterface::class);

// Run the app
$app->run();
```

Check out the [**Country Service**](https://github.com/tobento-ch/service-country) to learn more about the interfaces.

### Retrieve Countries

[](#retrieve-countries)

You can retrieve countries by using the `CountryRepositoryInterface::class`:

```
use Tobento\App\AppFactory;
use Tobento\Service\Country\CountryRepositoryInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);
$app->booting();

$countryRepository = $app->get(CountryRepositoryInterface::class);

// Run the app
$app->run();
```

Check out the [**Country Repository Interface**](https://github.com/tobento-ch/service-country#country-repository-interface) to learn more about it.

### Add Or Customize Countries

[](#add-or-customize-countries)

Currently, countries are available in the locales `en`, `de`, `fr` and `it`.

You might add or customize/override new countries by the following way:

```
use Tobento\App\AppFactory;
use Tobento\Service\Country\CountryRepositoryInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Add country directory with higher priority:
$this->app->dirs()->dir(
    dir: $this->app->dir('app').'countries-custom/',

    // do not use 'countries' as name for migration purposes
    name: 'countries.custom',

    group: 'countries',

    // add higher priority as default countries dir:
    priority: 300,
);

// Adding boots
$app->boot(\Tobento\App\Country\Boot\Country::class);

// Run the app
$app->run();
```

Then just add the countries files you wish to override in the defined directory. If the file does not exist, the file from the default country directory is used.

Learn More
----------

[](#learn-more)

### Using Countries In Forms

[](#using-countries-in-forms)

The following examples require the [App View](https://github.com/tobento-ch/app-view) bundle with the following boots:

```
// ...

$app->boot(\Tobento\App\View\Boot\View::class);
$app->boot(\Tobento\App\View\Boot\Form::class);

// ...
```

**Example using countries column**

```
use Tobento\Service\Country\CountryRepositoryInterface;
use Tobento\Service\Country\CountryInterface;
use Tobento\Service\View\ViewInterface;

$countryRepository = $app->get(CountryRepositoryInterface::class);
$countries = $countryRepository->findCountries(locale: 'de');

// You may filter countries:
$countries = $countries->only(['CH', 'US']);

$view = $app->get(ViewInterface::class);

$content = $view->render(view: 'countries/select', data: [
    'countries' => $countries->column(column: 'name', index: 'code'),
]);
```

The `views/countries/select.php` view:

```
