douhua1890 2018-11-20 17:41
浏览 38
已采纳

AppServiceProvider启动功能和动态标题

i passed variable to all views in AppServiceProvider boot() i stored in this variable the name of the first user in the database so i can put it in my layout as title

the problem happenes when there is no user in the database (new databases) it through exception which says $title is not defind and that is true it is not defind so i tried to make if statment but it ignore it for some reason

boot ()

  public function boot()
    {
      Schema::defaultStringLength(191);

      View::composer('*',function($view){
        $title = DB::table('users')->first();
        $view->with('title',$title);
      });

i tried this as the logic but didn't work

 public function boot()
    {
      Schema::defaultStringLength(191);

      $check = DB::table('users')->first();
        if ($check != null) {
          View::composer('*',function($view){
            $title = DB::table('users')->first();
            $view->with('title',$title);  });
        }else {
          return view('nouser');
        }

there is no syntax error but i think the problem is every time the website reloads it renders all the layouts ( i have 3) guest and admin have

<title>{{$title->name}}</title>

and app which the title is not dynamic in it and the nouser page i coded i extended the app layout bcs it doesn't have this variable which is undefined

the problem is i need to hide this exception from the user and redirect him to page that says please register and then i redirect him to the main pages after regiestration

  • 写回答

2条回答 默认 最新

  • douquejituan938904 2018-11-22 17:15
    关注

    i solved it by doing it in my pages controller insted of the appservice provider

    pages controller

    public function gethome()
      {
        $title = DB::table('users')->first();
          if ($title == null) {
            return redirect('/register');
          }else{
            $cvg = CV::all();
            return view('guest.cv')->with('cvg', $cvg);
      }
    

    and did it for all the guest directories which they are 4 main routes

    in AppServiceProvider

    public function boot()
        {
          Schema::defaultStringLength(191);
    
          View::composer('*',function($view){
          $title = DB::table('users')->first();
          $view->with('title',$title);
      });
        }
    

    in my guest layout

    <title>{{$title->name}}</title>
    

    hope this help anyone have the same problem Good luck and have fun coding

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

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序