Navitia Component is a PHP library to query Navitia 2 Api (http://api.navitia.io), and controls query parameters.
- PHP: >=7.4
- a Navitia token
Note: If you don't have a Navitia token yet, you have to register here: http://navitia.io/register
Presently this component exists in 2 major versions :
-
NavitiaComponent 1.x.x used by 'classic' projects in production
-
NavitiaComponent 2.x.x compatible with modern frameworks like Symfony4
This documentation is for NavitiaComponent 2.x.x
Using composer:
composer require "canaltp/navitia":"~2.0"
In services.yaml file of your app, add this :
navitia_component:
class: Navitia\Component\Service\ServiceFacade
factory: [Navitia\Component\Service\ServiceFacade, getInstance]
calls:
- [ setCache, ["@cache.app.taggable"]]
- [ setConfiguration, ["%navitia_config%"]]
And then add @navitia_component
to the service that used NavitiaComponent like this :
App\Service\Navitia:
class: App\Service\Navitia
arguments: ['@navitia_component']
Set configuration:
You can pass an array of config with the setConfiguration
function.
namespace App\Service;
use Navitia\Component\Service\ServiceFacade;
class Navitia
{
private ServiceFacade $navitiacomponent;
function __construct(ServiceFacade $navitiacomponent)
{
$this->navitiacomponent = $navitiacomponent;
// Configuration
$config = array(
'url' => 'api.navitia.io',
'token' => '3b036afe-0110-4202-b9ed-99718476c2e0', // This token has an access to sandbox data
);
$this->navitiacomponent->setConfiguration($config);
}
}
Name | Type | Description | Accepted values | Default value |
---|---|---|---|---|
url (required) |
string |
Base url of Navitia | ||
token (required) |
string |
Your token | ||
timeout |
int |
Navitia timeout (in ms) | 6000 |
|
version |
string |
Api version | v1 |
v1 |
format |
string |
Wanted output format | json , object |
object |
Query example:
$query = array(
'api' => 'journeys',
'parameters' => array(
'from' => '2.3749036;48.8467927',
'to' => '2.2922926;48.8583736',
),
);
$result = $this->navitiacomponent->call($query); // Returns an object with Api result
Config parameters:
Navitia component supports these apis:
- Journeys
- Coverage
- Departures
Example of an itinerary:
$query = array(
'api' => 'journeys',
'parameters' => array(
'from' => '2.3749036;48.8467927',
'to' => '2.2922926;48.8583736',
),
);
$result = $this->navitiacomponent->call($query);
See also: http://doc.navitia.io/#journeys
Example, retrieve metadata about a coverage:
$query = array(
'api' => 'coverage',
'parameters' => array(
'region' => 'sandbox',
),
);
$result = $this->navitiacomponent->call($query);
See also: http://doc.navitia.io/#coverage
Example, get all next departures of a line and at a datetime:
$query = array(
'api' => 'departures',
'parameters' => array(
'region' => 'sandbox',
'path_filter' => '/lines/line:RAT:M1/departures?from_datetime=20160615T1337'
),
);
$result = $this->navitiacomponent->call($query);
See also: http://doc.navitia.io/#departures
To query Navitia with any other url using this component and the config you have provided, this pattern do the trick:
$query = array(
'api' => 'coverage',
'parameters' => array(
'region' => 'sandbox', // Coverage name
'path_filter' => 'stop_areas?variable=value', // Url to call
),
);
$result = $this->navitiacomponent->call($query);
// Will call http://api.navitia.io/v1/coverage/sandbox/stop_areas?variable=value
You can use a query builder:
use Navitia\Component\Request\JourneysRequest;
use Navitia\Component\Request\Parameters\JourneysParameters;
$query = new JourneysRequest();
$actionParameters = new JourneysParameters();
$actionParameters
->setFrom('2.3749036;48.8467927')
->setTo('2.2922926;48.8583736')
->setDatetime('20160819T153000')
;
$query->setParameters($actionParameters);
$result = $this->navitiacomponent->call($query);
Using query builder to get all next departures:
use Navitia\Component\Request\Parameters\CoverageDeparturesParameters;
use Navitia\Component\Request\DeparturesRequest;
$query = new DeparturesRequest();
$query->setRegion('sandbox')->setPathFilter('lines/line:RAT:M1');
$actionParameters = new CoverageDeparturesParameters();
$actionParameters->setDuration(1);
$actionParameters->setFromDatetime('20160615T1337');
$actionParameters->setForbiddenUris(['lines', 'modes']);
$actionParameters->setDataFreshness('realtime');
$query->setParameters($actionParameters);
$result = $this->navitiacomponent->call($query);
For this part, you should use docker (need install) and launch it with :
make all_tests_dev
The library is under MIT.