AppServiceProvider boot function and Dynamic title
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
add a comment |
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
add a comment |
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
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
php laravel variables laravel-blade
asked Nov 20 '18 at 17:41
RYOK SecurityRYOK Security
456
456
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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..
}
}
?>
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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..
}
}
?>
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
add a comment |
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..
}
}
?>
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
add a comment |
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..
}
}
?>
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..
}
}
?>
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 22 '18 at 17:15
RYOK SecurityRYOK Security
456
456
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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