How to Route Multilingual Website

Posted 3 years ago by megadola

I'm working on simple CMS, and i'm facing some problems on how to create Route to work with the below structure:

domain.com (should display the main page with the default language)
domain.com/contact (should display the contact page with the default language)
domain.com/es  (should display the main page with the espanish language)
domain.com/es/contact  (should display the contact page with the espanish language)

important note: there are unlimited languages/pages, so i cannot create static routes for each language/page .. i have to use variables {lang}/{page}

Problem is, i'm getting this error while calling domain.com/es: Missing argument 2

my controller with static views:

namespace App\Http\Controllers;

use Illuminate\Support\Facades\App;

class PagesController extends Controller
{
    public function showPage($locale, $page)
    {
        if($locale):
            App::setLocale($locale);
        endif;
        switch($page)
        {
            case 'about':
                return view('pages.about');
            break;
            case 'products':
                return view('pages.products');
            break;
            case 'clients':
                return view('pages.clients');
            break;
            case 'services':
                return view('pages.services');
            break;
            case 'contact':
                return view('pages.contact');
            break;
            
            default:
                abort(404);
        }
    }
}

my route:


Route::get('/', function () {
    return view('pages.home');
});

/* load Pages */
Route::get('/{locale}', [
    'as' => 'page', 'uses' => 'PagesController@showPage'
]);

/* load Pages */
Route::get('/{locale}/{page}', [
    'as' => 'page', 'uses' => 'PagesController@showPage'
]);

thanks

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.