Validation with Internationalization in Laravel 5.3 presenting error

1

I'm creating an application where I have to validate the data sent to the bank (obvious), but I'm having the following error:

  

ErrorException in FileLoader.php line 109:

     

Object of class Illuminate \ Routing \ Router could not be converted to string

For you to understand better, I'll introduce my code:

routes / web.php

...
Route::group(['prefix' => 'dashboard/{locale}/'], function($locale) {

    if (empty($locale)) {
        $locale = 'pt-BR';
    }

    App::setLocale($locale);

    ...
    Route::get('api/add', 'ApiController@addApi');
    Route::post('api/add', 'ApiController@storeApi');

    ...
}

URL Example: /dashboard/pt-BR/api/add

app / Http / Controllers / ApiController.php

public function storeApi(Request $request)
{
    $validator = Validator::make(
        $request->all(),
        $this->rules, 
        $this->messages
    )->validate(); // Por padrão, em caso de erro, ele retornaria para o form com os erros.

    $api = $this->apiRepository->model()->create($request->all());

    // $this->locale = 'pt-BR' - É recuperado por um middleware...
    if ($api) {
        return redirect()->to(
            'dashboard/' . $this->locale .
            '/api/add-accounting/' . $api['id']
        );
    }
}

One thing I noticed

I accessed the path presented in the error, it being:

\path\vendor\laravel\framework\src\Illuminate\Translation\FileLoader.php
Linha: 109

And the method is this:

protected function loadPath($path, $locale, $group)
{
    if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) {
        return $this->files->getRequire($full);
    }

    return [];
}

If I put a dd() in each parameter, I have:

$path = 'path\resources\lang'
$locale = Router {#24 ▼
              #events: Dispatcher {#5 ▶}
              #container: Application {#3 ▶}
              #routes: RouteCollection {#26 ▶}
              #current: Route {#230 ▶}
              #currentRequest: Request {#40 ▶}
              #middleware: array:6 [▶]
              #middlewareGroups: array:2 [▶]
              +middlewarePriority: array:5 [▶]
              #binders: array:1 [▶]
              #patterns: []
              #groupStack: []
          }
$group = 'validation'

I particularly think that the problem should be in $locale but I could not accurately identify the error, because I forced it to $locale = "pt-BR" and the error changes to:

  

ErrorException in Translator.php line 273:

     

Illegal offset type

I imagined that it might be an error in the translation files, but I have validated it and everything is fine, but for any doubt, follow the same in github .

    
asked by anonymous 14.10.2016 / 21:11

1 answer

1

The problem is how you define your Route::group . This method is different from the ones that are used with Route::get , Route::post , and the like.

Although it also receives an anonymous function, the first argument received is an object of type Illuminate\Routing\Router (note what it returns in its dd ).

Something that might help you with what you want to do is use the Sub-domain Routing :

Route::group(['domain' => '{locale}.myapp.com', 'prefix' => 'dashboard/'], function () {

    Route::get('user/{id}', function ($locale, $id) {
        // Ao acessar pt-br.myapp.com/dashboard/user/1
        echo $locale; // pt-br
    });

});

This implies a change in the structure of your routes, but in my opinion it is a simpler way to achieve what you want.

From here you can set app()->setLocale() from a global middleware for example.

    
08.02.2017 / 01:34