request - How to grab URL path after locale "us_en" in Laravel 5.4? -


im new in laravel , im tryig grab in route, comes after locale ("us_en/").

this controler

<?php  namespace app\http\controllers\sitepublic;  use illuminate\http\request; use app\http\controllers\controller; use illuminate\support\facades\app;  class homecontroller extends controller {     public function index(request $request,$account,$location) {          dd($request->path());          $loc=explode("_", $location);         $locale = $loc[0];         $lang= $loc[1];          app::setlocale($lang);           return view('homeprofpublic')->with(['account' => $account,'active' => 'home','lang'=>$lang,"locale"=>$locale]);       }  } 

for using $request->path() grab in route.

my route map this

    mysite.com/useraccount                   =>user's home page english default language     mysite.com/useraccount/us_sp             =>user's home page in spanish      mysite.com/useraccount/us_sp/contact     =>user's contac page in spanish     ... 

so $request->path() give me full url , use php explode grab after locale (if case).

so question if there function or method done in laravel solve situation? if yes it?

you don't need use $request if set route properly: can receive parameters in controller function

you can set either 3 routes separate logic in different functions or 1 optional parameters

route::get('/useraccount/{language?}/{method?}', 'yourcontroller@index'); 

and then, in controller specify default values parameters:

public function index($language=null, $method=null) {     list($locale, $lang) = explode('_', $language);     var_dump($locale);     var_dump($lang);      var_dump($method); } 

and url /useraccount/us_sp/contact

string 'us' (length=2)

string 'sp' (length=2)

string 'contact' (length=7)


update can create middleware , process urls there

php artisan make:middleware processlanguageroutes 

you need register middleware in app/http/kernel.php file

protected $middlewaregroups = [     'web' => [        ...         \app\http\middleware\processlanguagemenu::class,     ], 

and then, can process data in handle function checking $request variable

public function handle($request, closure $next) {      if ($request) { // check         //     }      return $next($request); } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -