AppServiceProvider boot function and Dynamic title












0















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










share|improve this question



























    0















    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










    share|improve this question

























      0












      0








      0








      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










      share|improve this question














      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







      php laravel variables laravel-blade






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 17:41









      RYOK SecurityRYOK Security

      456




      456
























          2 Answers
          2






          active

          oldest

          votes


















          0














          The main problem on your code is; in case of there is no user you are trying to print something in the object. You should care S.O.L.I.D. principles when trying to implement something.



          $countUsers = DB::table('users')->count();
          $title = null;
          if($countUsers > 0){
          $title = DB::table('users')->first()->name;
          }
          View::composer('*',function($view){
          $view->with('title',$title);
          });
          if($countUsers == 0 ){
          return view('nouser');
          }


          and in your template use



          <title>{{$title}}</title>


          instead of



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


          Using View::composer is not a better solution to dynamically changing titles.
          So, you can use artesaos/seotools package for the dynamic titles, descriptions and much more easily. Also, it has got a dynamic twitter and opengraph meta generation feature which is really important for the seo.



          <?php
          ....
          class ForExampleController extends {
          public function viewUser($ID){
          $user = User::findOrFail($ID);
          SEO::setTitle($user->name);
          SEO::setDescription("This is the profile of ".$user->name);
          SEO::opengraph()->setUrl(...); // you can set users profile url
          // and much more..
          }
          }
          ?>





          share|improve this answer
























          • i replaced my code with yours first there is a syntax error the } that end the first case should be after the }); bcs if you put it like you said it says $title not defind second it didn't work after i replaced my code i redirected to home ('/') and threw the same exception as the last time

            – RYOK Security
            Nov 20 '18 at 18:32













          • i want to make declaration the main problem that i redirected to / the home page which uses guest layout which has the $title variable passed in and in this case it is not defined yet so i need to make if to check if it is not defined then return view no user and if it is defined then procced NOTE : i have more than 12 page which use the same guest layout thats why i put the logic in the boot function

            – RYOK Security
            Nov 20 '18 at 18:37













          • Why you preferred hardest way? You can use default middleware's of Laravel

            – bl4cksta
            Nov 20 '18 at 18:53











          • give me example if you can and i didn't use it because the guest interface should be available for everyone it is just one case that it should block access which is what i have explained above the middleware work for everyone and will block them all i mean it will work permenatly i just want a simple message to pop up ehen there is no user registered

            – RYOK Security
            Nov 20 '18 at 19:31



















          0














          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






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398599%2fappserviceprovider-boot-function-and-dynamic-title%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The main problem on your code is; in case of there is no user you are trying to print something in the object. You should care S.O.L.I.D. principles when trying to implement something.



            $countUsers = DB::table('users')->count();
            $title = null;
            if($countUsers > 0){
            $title = DB::table('users')->first()->name;
            }
            View::composer('*',function($view){
            $view->with('title',$title);
            });
            if($countUsers == 0 ){
            return view('nouser');
            }


            and in your template use



            <title>{{$title}}</title>


            instead of



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


            Using View::composer is not a better solution to dynamically changing titles.
            So, you can use artesaos/seotools package for the dynamic titles, descriptions and much more easily. Also, it has got a dynamic twitter and opengraph meta generation feature which is really important for the seo.



            <?php
            ....
            class ForExampleController extends {
            public function viewUser($ID){
            $user = User::findOrFail($ID);
            SEO::setTitle($user->name);
            SEO::setDescription("This is the profile of ".$user->name);
            SEO::opengraph()->setUrl(...); // you can set users profile url
            // and much more..
            }
            }
            ?>





            share|improve this answer
























            • i replaced my code with yours first there is a syntax error the } that end the first case should be after the }); bcs if you put it like you said it says $title not defind second it didn't work after i replaced my code i redirected to home ('/') and threw the same exception as the last time

              – RYOK Security
              Nov 20 '18 at 18:32













            • i want to make declaration the main problem that i redirected to / the home page which uses guest layout which has the $title variable passed in and in this case it is not defined yet so i need to make if to check if it is not defined then return view no user and if it is defined then procced NOTE : i have more than 12 page which use the same guest layout thats why i put the logic in the boot function

              – RYOK Security
              Nov 20 '18 at 18:37













            • Why you preferred hardest way? You can use default middleware's of Laravel

              – bl4cksta
              Nov 20 '18 at 18:53











            • give me example if you can and i didn't use it because the guest interface should be available for everyone it is just one case that it should block access which is what i have explained above the middleware work for everyone and will block them all i mean it will work permenatly i just want a simple message to pop up ehen there is no user registered

              – RYOK Security
              Nov 20 '18 at 19:31
















            0














            The main problem on your code is; in case of there is no user you are trying to print something in the object. You should care S.O.L.I.D. principles when trying to implement something.



            $countUsers = DB::table('users')->count();
            $title = null;
            if($countUsers > 0){
            $title = DB::table('users')->first()->name;
            }
            View::composer('*',function($view){
            $view->with('title',$title);
            });
            if($countUsers == 0 ){
            return view('nouser');
            }


            and in your template use



            <title>{{$title}}</title>


            instead of



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


            Using View::composer is not a better solution to dynamically changing titles.
            So, you can use artesaos/seotools package for the dynamic titles, descriptions and much more easily. Also, it has got a dynamic twitter and opengraph meta generation feature which is really important for the seo.



            <?php
            ....
            class ForExampleController extends {
            public function viewUser($ID){
            $user = User::findOrFail($ID);
            SEO::setTitle($user->name);
            SEO::setDescription("This is the profile of ".$user->name);
            SEO::opengraph()->setUrl(...); // you can set users profile url
            // and much more..
            }
            }
            ?>





            share|improve this answer
























            • i replaced my code with yours first there is a syntax error the } that end the first case should be after the }); bcs if you put it like you said it says $title not defind second it didn't work after i replaced my code i redirected to home ('/') and threw the same exception as the last time

              – RYOK Security
              Nov 20 '18 at 18:32













            • i want to make declaration the main problem that i redirected to / the home page which uses guest layout which has the $title variable passed in and in this case it is not defined yet so i need to make if to check if it is not defined then return view no user and if it is defined then procced NOTE : i have more than 12 page which use the same guest layout thats why i put the logic in the boot function

              – RYOK Security
              Nov 20 '18 at 18:37













            • Why you preferred hardest way? You can use default middleware's of Laravel

              – bl4cksta
              Nov 20 '18 at 18:53











            • give me example if you can and i didn't use it because the guest interface should be available for everyone it is just one case that it should block access which is what i have explained above the middleware work for everyone and will block them all i mean it will work permenatly i just want a simple message to pop up ehen there is no user registered

              – RYOK Security
              Nov 20 '18 at 19:31














            0












            0








            0







            The main problem on your code is; in case of there is no user you are trying to print something in the object. You should care S.O.L.I.D. principles when trying to implement something.



            $countUsers = DB::table('users')->count();
            $title = null;
            if($countUsers > 0){
            $title = DB::table('users')->first()->name;
            }
            View::composer('*',function($view){
            $view->with('title',$title);
            });
            if($countUsers == 0 ){
            return view('nouser');
            }


            and in your template use



            <title>{{$title}}</title>


            instead of



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


            Using View::composer is not a better solution to dynamically changing titles.
            So, you can use artesaos/seotools package for the dynamic titles, descriptions and much more easily. Also, it has got a dynamic twitter and opengraph meta generation feature which is really important for the seo.



            <?php
            ....
            class ForExampleController extends {
            public function viewUser($ID){
            $user = User::findOrFail($ID);
            SEO::setTitle($user->name);
            SEO::setDescription("This is the profile of ".$user->name);
            SEO::opengraph()->setUrl(...); // you can set users profile url
            // and much more..
            }
            }
            ?>





            share|improve this answer













            The main problem on your code is; in case of there is no user you are trying to print something in the object. You should care S.O.L.I.D. principles when trying to implement something.



            $countUsers = DB::table('users')->count();
            $title = null;
            if($countUsers > 0){
            $title = DB::table('users')->first()->name;
            }
            View::composer('*',function($view){
            $view->with('title',$title);
            });
            if($countUsers == 0 ){
            return view('nouser');
            }


            and in your template use



            <title>{{$title}}</title>


            instead of



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


            Using View::composer is not a better solution to dynamically changing titles.
            So, you can use artesaos/seotools package for the dynamic titles, descriptions and much more easily. Also, it has got a dynamic twitter and opengraph meta generation feature which is really important for the seo.



            <?php
            ....
            class ForExampleController extends {
            public function viewUser($ID){
            $user = User::findOrFail($ID);
            SEO::setTitle($user->name);
            SEO::setDescription("This is the profile of ".$user->name);
            SEO::opengraph()->setUrl(...); // you can set users profile url
            // and much more..
            }
            }
            ?>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 18:13









            bl4ckstabl4cksta

            374414




            374414













            • i replaced my code with yours first there is a syntax error the } that end the first case should be after the }); bcs if you put it like you said it says $title not defind second it didn't work after i replaced my code i redirected to home ('/') and threw the same exception as the last time

              – RYOK Security
              Nov 20 '18 at 18:32













            • i want to make declaration the main problem that i redirected to / the home page which uses guest layout which has the $title variable passed in and in this case it is not defined yet so i need to make if to check if it is not defined then return view no user and if it is defined then procced NOTE : i have more than 12 page which use the same guest layout thats why i put the logic in the boot function

              – RYOK Security
              Nov 20 '18 at 18:37













            • Why you preferred hardest way? You can use default middleware's of Laravel

              – bl4cksta
              Nov 20 '18 at 18:53











            • give me example if you can and i didn't use it because the guest interface should be available for everyone it is just one case that it should block access which is what i have explained above the middleware work for everyone and will block them all i mean it will work permenatly i just want a simple message to pop up ehen there is no user registered

              – RYOK Security
              Nov 20 '18 at 19:31



















            • i replaced my code with yours first there is a syntax error the } that end the first case should be after the }); bcs if you put it like you said it says $title not defind second it didn't work after i replaced my code i redirected to home ('/') and threw the same exception as the last time

              – RYOK Security
              Nov 20 '18 at 18:32













            • i want to make declaration the main problem that i redirected to / the home page which uses guest layout which has the $title variable passed in and in this case it is not defined yet so i need to make if to check if it is not defined then return view no user and if it is defined then procced NOTE : i have more than 12 page which use the same guest layout thats why i put the logic in the boot function

              – RYOK Security
              Nov 20 '18 at 18:37













            • Why you preferred hardest way? You can use default middleware's of Laravel

              – bl4cksta
              Nov 20 '18 at 18:53











            • give me example if you can and i didn't use it because the guest interface should be available for everyone it is just one case that it should block access which is what i have explained above the middleware work for everyone and will block them all i mean it will work permenatly i just want a simple message to pop up ehen there is no user registered

              – RYOK Security
              Nov 20 '18 at 19:31

















            i replaced my code with yours first there is a syntax error the } that end the first case should be after the }); bcs if you put it like you said it says $title not defind second it didn't work after i replaced my code i redirected to home ('/') and threw the same exception as the last time

            – RYOK Security
            Nov 20 '18 at 18:32







            i replaced my code with yours first there is a syntax error the } that end the first case should be after the }); bcs if you put it like you said it says $title not defind second it didn't work after i replaced my code i redirected to home ('/') and threw the same exception as the last time

            – RYOK Security
            Nov 20 '18 at 18:32















            i want to make declaration the main problem that i redirected to / the home page which uses guest layout which has the $title variable passed in and in this case it is not defined yet so i need to make if to check if it is not defined then return view no user and if it is defined then procced NOTE : i have more than 12 page which use the same guest layout thats why i put the logic in the boot function

            – RYOK Security
            Nov 20 '18 at 18:37







            i want to make declaration the main problem that i redirected to / the home page which uses guest layout which has the $title variable passed in and in this case it is not defined yet so i need to make if to check if it is not defined then return view no user and if it is defined then procced NOTE : i have more than 12 page which use the same guest layout thats why i put the logic in the boot function

            – RYOK Security
            Nov 20 '18 at 18:37















            Why you preferred hardest way? You can use default middleware's of Laravel

            – bl4cksta
            Nov 20 '18 at 18:53





            Why you preferred hardest way? You can use default middleware's of Laravel

            – bl4cksta
            Nov 20 '18 at 18:53













            give me example if you can and i didn't use it because the guest interface should be available for everyone it is just one case that it should block access which is what i have explained above the middleware work for everyone and will block them all i mean it will work permenatly i just want a simple message to pop up ehen there is no user registered

            – RYOK Security
            Nov 20 '18 at 19:31





            give me example if you can and i didn't use it because the guest interface should be available for everyone it is just one case that it should block access which is what i have explained above the middleware work for everyone and will block them all i mean it will work permenatly i just want a simple message to pop up ehen there is no user registered

            – RYOK Security
            Nov 20 '18 at 19:31













            0














            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






            share|improve this answer




























              0














              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






              share|improve this answer


























                0












                0








                0







                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 17:15









                RYOK SecurityRYOK Security

                456




                456






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398599%2fappserviceprovider-boot-function-and-dynamic-title%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                    Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                    A Topological Invariant for $pi_3(U(n))$