duanraotun1674 2017-06-04 04:05 采纳率: 100%
浏览 151
已采纳

将路径文件中的指定参数传递给laravel中的控制器

In laravel routes file I have written this :

Route::get('/{lang}/{page}', 'PagesController@get' )->where('lang' , $langPattern );

and in pages controller I wrote this :

public function get($lang,$page)
{
     // do something
}

But I want only to use page parameter

I created a middleware to select language and there is no need for $lang in controllers

How can I remove it ?

Can I write like this :

public function get($page)
{
     // do something
}

My language middleware :

public function handle($request, Closure $next )
{
    // URL language
    $urllang = $request->segment(1);

    if ($request->session()->has("lang")){
        $sessionLang = $request->session()->get('lang');


        // if requested url hasn't language
        if ($urllang == NULL) {
            $urllang = $sessionLang;
            return redirect('/' . $sessionLang);
        }

        // if user change the language 
        elseif ($urllang != NULL && $urllang != $sessionLang) {
            $request->session()->put('lang' , $urllang);
        }
    }
    else{
        // if there is no session lang and no url lang
        // get app main lang from config file
        // Put session and redirect
        if ($urllang == NULL) {
            $request->session()->put('lang' , config('app.locale'));
            return redirect('/' . config('app.locale'));
        }
        // if there is no session 
        // Only put session
        else{
            $request->session()->put('lang' , $urllang);
        }
    }


    // get requested language id from database
    $lang = Lang::select("id" , "short" , "name" , "title" , "keywords" , "description" , "copyrights" , "site_name")->where( ['short' => $urllang , 'active' => 1 ])->first();
    // if requested language is not exist
    if (!$lang) {
        $request->session()->put('lang' , config('app.locale'));
        abort(404);
    }else{
        Lang::set($lang);
        return $next($request);
    }

}

Thanks .

  • 写回答

2条回答 默认 最新

  • douye7033 2017-06-06 19:18
    关注

    Finally i found the best solution Just i added to my language middleware this line

    $request->route()->forgetParameter('lang');
    

    I asked it to remove lang parameter from request variable .

    Hope to help you .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化