PHP code example of cariboufute / locale-route

1. Go to this page and download the library: Download cariboufute/locale-route library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cariboufute / locale-route example snippets


LocaleRoute::get('route', 'Controller@getAction', 'url');

/*
    This will give these routes.

    ['fr.route']    =>  'fr/url'
    ['en.route']    =>  'en/url'
*/

//Example with URL parameter and trailing where method
LocaleRoute::get('show', 'Controller@show', ['fr' => 'url_fr/{id}', 'en' => 'url_en/{id}'])
    ->where(['id' => '[1-9]');
/*
    These routes will exist...

    ['fr.route']    =>  'fr/url_fr/1'
    ['en.route']    =>  'en/url_en/1'

    ...but not these routes.
    ['fr.route']    =>  'fr/url_fr/0'
    ['en.route']    =>  'en/url_en/0'
 */

/**
 * Here, the '/' URL will be discarded. Only "/fr" and "/en" will exist.
 */

Route::get('/', function () {
    return redirect('/fr');
});

LocaleRoute::get('index', 'PublicController@index', ['fr' => '/', 'en' => '/']);


/**
 * Here, all routes with work fine : "/", "/fr" and "/en".
 */

LocaleRoute::get('index', 'PublicController@index', ['fr' => '/', 'en' => '/']);

Route::get('/', function () {
    return redirect('/fr');
});

 
// routes/web.php or app/Http/routes.php

LocaleRoute::resource('article', 'ArticleController');

/*
Will give these routes :

[fr.article.index]  => GET/HEAD     "/fr/article"                   => ArticleController::index()
[en.article.index]  => GET/HEAD     "/en/article"                   => ArticleController::index()
[fr.article.show]   => GET/HEAD     "/fr/article/{article}"         => ArticleController::show()
[en.article.show]   => GET/HEAD     "/en/article/{article}"         => ArticleController::show()
[fr.article.create] => GET/HEAD     "/fr/article/create"            => ArticleController::create()
[en.article.create] => GET/HEAD     "/en/article/create"            => ArticleController::create()
[article.store]     => POST         "/article"                      => ArticleController::store()
[fr.article.edit]   => GET/HEAD     "/fr/article/{article}/edit"    => ArticleController::edit()
[en.article.edit]   => GET/HEAD     "/en/article/{article}/edit"    => ArticleController::edit()
[article.update]    => PUT/PATCH    "/article/{article}"            => ArticleController::update()
[article.destroy]   => DELETE       "/article/{article}"            => ArticleController::destroy()
*/

// resources/lang/fr/route-labels.php

return [
    'create' => 'creer',
    'edit' => 'editer',
];

// resources/lang/en/route-labels.php

return [
    'create' => 'create',
    'edit' => 'edit',
];

// routes/web.php or app/Http/routes.php
LocaleRoute::resource('article', 'ArticleController');

/*
Will give these routes :

[fr.article.create] => GET/HEAD     "/fr/article/creer"             => ArticleController::create()
[en.article.create] => GET/HEAD     "/en/article/create"            => ArticleController::create()
[fr.article.edit]   => GET/HEAD     "/fr/article/{article}/editer"  => ArticleController::edit()
[en.article.edit]   => GET/HEAD     "/en/article/{article}/edit"    => ArticleController::edit()
...
*/
 
  // routes/web.php or app/Http/routes.php
  
  LocaleRoute::apiResource('article', 'ArticleController');
  
  /*
  Will give these routes :
  
  [fr.article.index]  => GET/HEAD     "/fr/article"                   => ArticleController::index()
  [en.article.index]  => GET/HEAD     "/en/article"                   => ArticleController::index()
  [fr.article.show]   => GET/HEAD     "/fr/article/{article}"         => ArticleController::show()
  [en.article.show]   => GET/HEAD     "/en/article/{article}"         => ArticleController::show()
  [article.store]     => POST         "/article"                      => ArticleController::store()
  [article.update]    => PUT/PATCH    "/article/{article}"            => ArticleController::update()
  [article.destroy]   => DELETE       "/article/{article}"            => ArticleController::destroy()
  */
  
config/app.php
 php
// config/app.php

'providers' => [
    //...
    CaribouFute\LocaleRoute\LocaleRouteServiceProvider::class,
    //...
],

'aliases' => [
    //...
    'LocaleRoute' => CaribouFute\LocaleRoute\Facades\LocaleRoute::class,
],
 php
// app/Http/Kernel.app

protected $middlewareGroups = [
    'web' => [
        //...
        \CaribouFute\LocaleRoute\Middleware\SetLocale::class,
    ],

    //...
];
 bash
php artisan vendor:publish
 bash
php artisan vendor:publish --provider "CaribouFute\LocaleRoute\LocaleRouteServiceProvider"
config/localeroute.php
config/localeroute.php
 php
// config/localeroute.php



return [

    /**
     * The locales used by routes. Add all
     * locales needed for your routes.
     */
    'locales' => ['fr', 'en'],

    /**
     * Option to add '{locale}/' before given URIs.
     * For LocaleRoute::get('route', ...):
     * true     => '/fr/route'
     * false    => '/route'
     * Default is true.
     */
    'add_locale_to_url' => true,
];

 php
    'locales' => ['en', 'fr', 'es', 'de'],
 php
    'add_locale_to_url' => false,
routes/web.php
app/Http/routes.php
 php
// routes/web.php or app/Http/routes.php

LocaleRoute::get('route', 'Controller@getAction', ['fr' => 'url_fr', 'en' => 'url_en']);
LocaleRoute::post('route', 'Controller@postAction', ['fr' => 'url_fr', 'en' => 'url_en']);
LocaleRoute::put('route', 'Controller@putAction', ['fr' => 'url_fr', 'en' => 'url_en']);
LocaleRoute::patch('route', 'Controller@patchAction', ['fr' => 'url_fr', 'en' => 'url_en']);
LocaleRoute::delete('route', 'Controller@deleteAction', ['fr' => 'url_fr', 'en' => 'url_en']);
LocaleRoute::options('route', 'Controller@optionsAction', ['fr' => 'url_fr', 'en' => 'url_en']);
LocaleRoute::any('route', 'Controller@getAction', ['fr' => 'url_fr', 'en' => 'url_en']);

 php
Route::get('fr/url_fr', ['as' => 'fr.route', 'uses' => 'Controller@getAction']);
Route::get('en/url_en', ['as' => 'en.route', 'uses' => 'Controller@getAction']);
resources/lang/{locale}/routes.php
routes.php
 php
// routes/web.php or app/Http/routes.php

LocaleRoute::get('route', 'Controller@routeAction');
 php
// resources/lang/en/routes.php

return [
    'route' => 'url_en',
]
 php
// resources/lang/fr/routes.php

return [
    'route' => 'url_fr',
]
"/en"
"/en"
 php
//routes/web.php or app/Http/routes.php

LocaleRoute::get('route', 'Controller@getAction', ['fr' => 'url_fr', 'en' => 'url_en', 'middleware' => 'guest']);

//To use trans files URL, just add 'middleware'
LocaleRoute::get('route', 'Controller@getAction', ['middleware' => 'guest']);

 php
// routes/web.php or app/Http/routes.php


Route::group(['as' => 'article.', 'prefix' => 'article'], function () {
    LocaleRoute::get('create', 'ArticleController@index', ['fr' => 'creer', 'en' => 'create']);
    Route::post('store', ['as' => 'store', 'uses' => 'ArticleController@store']);
});

/*
Will give these routes :

[fr.article.create] => GET  "/fr/article/creer"     => ArticleController::create()
[en.article.create] => GET  "/en/article/create"    => ArticleController::create()
[article.store]     => POST "/article/store"        => ArticleController::store()
*/
 php
/*
    Config::get('localeroute.locales') => ['fr', 'en']
    Config::get('localeroute.add_locale_to_url') => true
*/

LocaleRoute::get('index', 'Controller@index', ['fr' => '/', 'en' => '/']);

/*
    ['fr.index']    => '/fr'
    ['en.index']    => '/en'
*/

LocaleRoute::get('create', 'Controller@create', [
        'fr' => 'creer',
        'en' => 'create',
        'de' => 'erstellen',
        'locales' => ['fr', 'en', 'de']
]);

/*
    ['fr.create']    => '/fr/creer'
    ['en.create']    => '/en/create'
    ['de.create']    => '/de/erstellen'
*/

LocaleRoute::get('store', 'Controller@store', [
        'fr' => 'stocker',
        'en' => 'store',
        'add_locale_to_url' => false
]);

/*
    ['fr.store']    => '/stocker'
    ['en.store']    => '/store'
*/