Print database values in the main layout - php

I am using laravel 5 for my project. I have a view file layouts/app.blade.php which is extended by all the views. I have stored the application name , email in the database .... Now I need to show those data in my app.blade.php..
How do I do it?

Well as #DutGriff said, it seems to be that you are too new to laravel framework. Just dig in a bit and you get something called Service Injection in blade. Now, answer to your question:
Open you app.blade.php
Go ahead and type the following at very start of app.blade.php
#inject('model_name', 'App\ModelName')
{{{ $model_name = $model_name->where('app_name', 'app')->get() }}}
And then, just access it anywhere like:
{{ $model_name->name }}

Related

symfony 3 url issue

Hi I'm coming for a very simple question (I think) but i didn't found the answer or a similar case.
I'm using symfony 3 and trying to build a second menu for my administration pannel.
However, I have a problem about how I have to declare the relative url in my "href", For my main menu i used to do like this
{{ url ( 'admin' ) }}
and it worked. The fact is that now I have subfolders and many levels in my url.
The url i try to reach is myapp/admin/gameadmin, this url work when I'm going on it but when i try to put it in 'href' I have an error message which says that the route is not working.
i declared it like that ->
{{ url(admin/gameadmin) }}
I tried with different character -> admin:gameadmin, admin\gameadmin ... etc and with path instead of url i don't know if it's not the good way to declare it or if I have a problem with my controllers.
In my bundle it's organised like that :
->Controllers(folder)
->admin(folder) (You can also find my main controllers on this level)
->admingamecontroller (Where the page I try to reach is routed)
I hope i gave you all the informations, thank you for your help and sorry for my english !
The url parameter is not the the url per se (ie: admin/gameadmin), this is the route name, defined in your routing.yaml file, or in your controller annotation.
If your action is something like this:
/**
* #Route("/admin/gameadmin", name="gameadmin")
*/
public function gameAdminAction()
{
...
}
Then, to generate the route, you have to do this:
{{ url('gameadmin') }}
By doing this, all the links on your website will be up to date if you change the gameadmin url, as long as you don't change the route name.
I suggest you to read this documentation on the Symfony website: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
Edit: As pointed by user254319, if you're not using annotations, you'll have to edit your routing.yaml config file.
gameadmin:
path: /admin/gameadmin
controller: App\Controller\Admin\AdminGameController::gameadminAction
The route name is the yaml key: gameadmin.
Related Symfony documentation: https://symfony.com/doc/current/routing.html

Laravel - how can make dynamic content with layout?

I'm wanting to create a website with laravel framework. I had made layout but now, have some zone i don't know how to set content for it. Ex: 2 zone of me are left-menu and cart (please view picture). My left-menu will get content from table: categories and cart will get content from package cart [Cart::content()].
It's on layout and of course, all page will have it. But i don't know how to give content of categories and cart() for it. Please help me
I think that you should to use View Composer.
https://laravel.com/docs/5.6/views#view-composers
Use Blade templates, as found here: https://laravel.com/docs/5.6/blade
Wherever in your page you want to print content, use the {{ $mycontent }} construct. You can also use confitionals and loop structures like #if and #foreach to loop through collections.
Then, in your controllers, you can just call the view and pass it content from your database or wherever you get it by doing something like:
return response()->view(“myView”, [“mycontent” => $content], $httpStatus);
You may opt for afterMiddleware if you want it on every page. Create a section on the master blade page (usually app.blade.php) and fill it in the middleware just like you would in any other controller. You can create a middleware by running php artisan create:middleware Cart. A file will be created at app/Http/Middleware/Cart.php.
Register the middleware in the app/Http/kernel.php file.
You may have to add a Auth::check() condition to avoid errors.

How to use Vue with Laravel 5.4 (best practice)

I'm a software engineer and don't have a lot of experience with PHP or javascript. I will upgrading my skills in this so i started with a PHP framework Laravel which looks very nice in my opinion. I'm familar with the MVC principal so i can find my way in Laravel!
I created some models with corresponding controllers. I also created some blade views to display the data. I give a model object to the blade view as folows:
$person = new \App\Person("Firstname", "Lastname");
return view('personView', compact('person'));
I can get it done to use the person object in the blade view to display his variables with:
{{ $user->name }}
Now i tried to go one step further and want do do some stuff with Vue so i can display and manipulate the object on client side (and dispay dynamic data). Later i want to save the client side object via a action in the controller over a HTTP post or something, but fist things first...
I'm be able to create a vue object with some data in it and display it in HTML controlls for example.
So, summerized
i'm be able to generate my backend objects and manipulate them. I can bring them to my view and display them with {{ $obj->var }}
I'm be able to create Vue objects with data, and manipulating this data and show it with #{{ data }}.
My problem is that i can't get the data from the controller (which i give wat i gave to the view) into the Vue object. What is the best practice to do this? Or is this not the way to go? in that case, what is convinient to do as i read that Laravel has Vue integrated and this sounds for me that there should be a (best practice) way to achieve this?
I looked for a creat tutorial but couln'd find this. So if someone has a good tutorial to learn understanding Laravel in combination with Blade and Vue, please share!
I already tried something like this, unfortunately without any result:
new Vue({
el: '#someElement',
data: {
person: {!! $person!!}
}
})
(Using Laravel 5.4.x and Vue 2.4.x)
You can simply pass the object to the vue component like that:
<my-component :person="{{ $person }}" ></my-component>
And your Vue component
<template>
{{ person.name }}
</template>
<script>
export default {
props: ['person']
}
</script>

How To Include PHP Files In Twig Files In Symfony2

I am trying to insert a latest tweets "widget" on to a Symfony2 project. I have found an ideal script written in PHP that will do the job perfectly.
However, I don't know where the best place to put 3rd party PHP files in a Symfony2 project. I have placed them in the same folder as all my twig files reside, changed the name to read tweets.php.twig, and even located them in the web folder. When I try to include the file in the twig file that needs the Twitter feed it comes up with an error saying that it can't find the file.
Do I have the right idea, or do I have to convert the PHP in to a twig file or write the PHP script in to a controller?
I believe the recommended way would be to create a Symfony2 bundle that encapsulates all the logic for the tweet widget. You would then call your bundle controller and pass the response to your twig template.
If that is too complicated or you want something more quick and dirty - you can create a controller like TweetWidgetController.php and put the code into there as an action like widgetAction. Just make sure you return the tweet widget output in a Symfony response object.
Then from your main controller - you can do something like
$widget = $this->forward('YourBundle:TweetWidget:widget', array('twitterid' => 'yourtwitterid'));
return $this->render('YourBundle:yourtemplate.html.twig',array('widget' => $widget->getContent()));
Now in your twig template you can put it wherever you'd like by referencing it as:
{{ widget }}

Templating with Twig - permanent access to a variable in all my templates

I've just started looking into Twig and I'm wondering how I would accomplish the following.
I have a variable $logged_in that I need to have access to in every single page on my site, I was hoping that rather than passing this to the twig renderer every single time in the data array, there would be a way for me to declare this somewhere, and for every template to have access to it.
Do I need to build an extension to accomplish this / or is it even possible? I have looked through every page of the documentation but I'm having trouble having tried to extend the base template as described here...
Twig Documentation | Recipes | Making the Templates aware of the Context Dead link
Is this the right approach?
Thanks
Just read about the new features in the 1.0RC release which should help.
Taken from the blogpost:
Globals:
PHP
// a global can be a constant
$twig->addGlobal('pi', 3.14);
// or any other valid PHP expression, like an object
$twig->addGlobal('request', new Request());
Template
{{ pi }}
{{ request.params('name') }}

Categories