laravel - Routes using annotations - passing custom variables and translation -
laravel - Routes using annotations - passing custom variables and translation -
using routes.php
it's quite easy create paths different languages using prefixes, illustration can create page routes about
, pl/o-nas
urls same route using:
if (\request::segment(1) =='pl') { $prefix ='pl'; \app::setlocale('pl'); } else{ $prefix =''; \app::setlocale('en'); } route::group( array('prefix' => $prefix, function () { route::any('/'.trans('routes.about'), 'controller@action'); } );
but know laravel 5 default uses annotation. possible accomplish same using annotations? @ moment there not many info using annotations in laravel 5.
you can first add together routeserviceprover
before
method similar code:
if (\request::segment(1) =='en') { $routeprefix =''; $this->app->setlocale('en'); } else{ $routeprefix ='pl'; $this->app->setlocale('pl'); }
but how can utilize prefix , translation in annotations , how utilize trans function here? should won't work because cannot set function annotation , don't know if there way add together prefix here.
/** * @get("/trans('routes.about')") * @prefix: {$prefix} */ public function about() { homecoming "about page"; }
something should work:
<?php namespace app\http\middleware; utilize closure; utilize illuminate\contracts\routing\middleware; class langdetection implements middleware { /** * handle incoming request. * * @param \illuminate\http\request $request * @param \closure $next * @return mixed */ public function handle($request, closure $next) { if ($request->segment(1) =='en') { $this->app->setlocale('en'); } else{ $this->app->setlocale('pl'); } homecoming $next($request); } }
then create sure run on every phone call - set application middleware stack (app/providers/appserviceprovider.php):
protected $stack = [ 'app\http\middleware\langdetection', 'illuminate\cookie\middleware\guard', 'illuminate\cookie\middleware\queue', 'illuminate\session\middleware\reader', 'illuminate\session\middleware\writer', ];
edit: alternatively dont have set in stack, can leave 'middleware filter' , phone call on specific routes - same 'auth', 'csrf' etc
laravel annotations laravel-routing laravel-5 laravel-annotations
Comments
Post a Comment