I have created a module in Laravel and im using views on that module, my structure is this:
Modules
-> MyModule
->->Controllers
->->Views
->->->MyModule.blade.php
But i have the headers and footer done on resources->views->layouts->base.blade.php
So how can i call this one so i can use the same base layout in all modules? it is possible on Laravel 5?
Already tried this
#include('layouts.base')
but im getting
Trying to get property of non-object (View: ... resources\views\layouts\base.blade.php
Thank you.
The structure of blade is relative to the views folder in the the resources folder.
Thus making your #include() have a structure like the this:
#include('DIRECTORY.BLADE')
and you can include your various blade contents by using #yield()
#yield('YIELD_FIELD_NAME')
If you are trying to have blades extent from that layout you would call that at the top of the blade files you want to extend off it.
#extends('DIRECTORY.BLADE')
This is an example blade file that can extend your layout if your layout contains the #yield('content') tag in it.
example.blade.php
#extends('layouts.base')
#section('content')
YOUR BLADES HTML/CONTENT
#endsection
https://laravel.com/docs/5.4/blade#defining-a-layout
How to add Auth middleware to controller:
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
Here is an example Controller:
https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/UsersManagementController.php
Here is an example of a view that uses that controller:
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/usersmanagement/show-user.blade.php
Here is an example of the template that view uses:
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/layouts/app.blade.php
Here is the routing file for the above examples:
https://github.com/jeremykenedy/laravel-auth/blob/master/routes/web.php
Ok i was abble to get the Auth work on Module structure, just need to add this on your routes.php
Route::group(['middleware' => ['web']], function () {
//Your routes
});
Related
I am working on Laravel for the first time
i have to make a Front End Menu Dynamic in Header and Footer [ list of categories will come from database ]. which controller I have to use for this.?
any common controller available in this framework to send data to header and footer.
When I receive the data in HomeController index Action its available for the home page only.
class HomeController {
public function index()
{
$categories = Category::get();
return view('home', compact('categories'));
}
}
Thanks.
This is a perfect case for View Composers:
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method:
View::composer(['partials.header', 'partials.footer'], function ($view) {
$view->with('categories', [1, 2, 3]); // bind data to view
});
Now you could simply retrieve $categories within your view (blade template).
Tip: Common practice is to create a new service provider called ComposerServiceProvider and place the abovementioned composer logic in the boot() method.
I assume you are using the Header and Footer in a master layout file. In this case you need to send all header/footer info every request. Which would be silly so instead use View Composers:
Define them in your appServiceProvider in the boot() method
view()->composer('home', function($view) {
$view->with('categories', App\Category::all());
});
In my example i made 'home' the name of the view, since it is in your example. But i would make a master file called layout and include a header and footer partial. If you want categories inside your header you could make a view()->composer('layout.header') with only header data.
which controller I have to use for this.?
Any
any common controller available in this framework to send data to header and footer
No.
You control what is returned from the Controller to be the response. You can design layouts and break them up into sections and have views that extend them. If you setup a layout that has the header and footer and your views extend from it, you can use a View Composer to pass data to that layout when it is rendered so you don't have to do this every time you need to return a view yourself.
Laravel Docs 5.5 - Front End - Blade
Laravel Docs 5.5 - Views - View Composers
In case of this u can use components
https://laravel.com/docs/8.x/blade#components
php artisan make:component Header
View/Component.Header.php
public function render()
{
$category = [Waht you want];
return view('views.header', ['category'=>$category]);
}
Then include Header.php to your blade like this
views/front.blade.php
<html>
<body>
<x-header/> <!-- like this -->
#yield('content')
#include('footer')
<body>
I changed the files path after i installed laravel framework like this:
from:
resources/views/welcome.bandle.php
to
resources/views/admin/index.php
and the routes file to:
Route::get('/admin', function () {
return view('admin/index');
});
the url is working
but all the larvael render not working
like this:
what i need to do?
tnx a lot.
You need to add the .blade.php extension to the files you want to parse using "Blade Engine", that will remove all the tags you have within curly braces.
Next, you need to write your route like this:
Route::get('/admin', function(){
return view('admin.index');
})->name('admin.index')->middleware('auth');
It is a good convention naming the routes for easy access across the application, that way you can simply reference it in the blade views like this:
Admin page
That way you will have the dynamic route no matter from where in the file structure you call it.
Or you can also use your Controller to display such view. By this you'll write your routes more cleaner. Let's say we have an AdminController that handles all admin processes and functions. Put your dashboard.blade.php inside views/admin directory.
The route:
Route::get('/admin', 'AdminController#index');
The controller:
class AdminController extends Controller
{
public function index()
{
return view('admin.dashboard'); // in views->admin->dashboard.blade.php
//add some data here
}
}
Just keep 'blade' in view file name if you don't plan to use controller, e.g.:
resources/views/admin/index.blade.php
In my app, i divided the folders in my view by:
"posts"
"pages"
"partials"
layout.blade.php
In my folder partials i have blade files, like nav.blade.php and footer.blade.php. In my folder "pages", i have files like home.blade.php and some other internal pages.
But now i need to pass a viable from a controller to my footer, since is needed to be shared in all views.
How i pass the variable from my PagesController that manages blade files inside the folder "pages" in the footer?
Example routeS:
Route::get('/', 'PageController#getHome');
Route::get('/registar', 'AuthenticationController#getRegistration');
Route::post('/post/comment', 'CommentController#store');
Route::get('/login', 'AuthenticationController#getLogin');
If i understood correctly, you want to share some data every time particular view is called? If that so, you can do this:
/* App\Providers\AppServiceProvider.php
** Edit the boot function, (dont forget to include View facade) */
public function boot(){
View::composer('path_to_view', function($view)
{
$view->with('variable', $variable); // you can pass array here aswell
});
}
Other way to do this (if you are extending the footer layout from the view you are accessing), is to pass data when you extend the layout, like this:
#extends('footer', ['data' => $data])
If you need to share same variable in multipe view from providers. you can also try to this
public function boot()
{
//set view path in array
View::composer(['path_to_view1', 'path_to_view2'], function ($view) {
$view->with('variableName', $variableName);
});
}
Currently we are using the Laravel framework on several projects, but one issue we keep running into which i don't like is the following issue:
Lets say you have a Homepage and a Content page
HomepageController has all Homepage specific php code
ContentpageController has all Content specific php code
we have an app.blade.php that does
#yield('page')
HomepageController calls the view homepage.blade.php containing
#extends('app')
#section('page')
Some HTML part
#include('parts.top_5')
#endsection
ContentController calls the view content.blade.php containing
#extends('app')
#section('page')
Some different HTML part
#include('parts.top_5')
#endsection
Here you can see both pages include parts.top_5, the top 5 needs some specific variables to output the top5. Now the issue is we are currently copying the code for the top5 variables in both controllers or in grouped middleware but is there a better solution to generate some blade specific variables when the part is included? So a bit like running a controller function on loading the blade template?
I have been searching the internet but can't seem to find anyone with the same question. Hopefully someone can help me on this mindbreaking issue!
You can add this binding to AppServiceProvider
(or any custom ServiceProvider You want)
like this:
public function boot()
{
$view->composer('parts.top_5', function($view) {
$view->with('any_data' => 'You want');
})
}
This way any time Laravel will compose parts.top_5 view this closure method will be triggerd.
And in documentations it's here:
http://laravel.com/docs/5.0/views#view-composers
I'm learning Laravel, and for my first project I'd like to create my portfolio. However, the first task I have to do is confusing me.
So I created my templates, layout.blade.php and home.blade.php. That makes sense to me, but now how do I tell Laravel, or how do I route to home.blade.php?
I'm looking for an explanation rather then just code. I'm trying to learn.
Actually, a view in MVC application is just a part of the application and it's only for presentation logic, the UI and one doesn't call/load a view directly without the help of another part (controller/function) of the application. Basically, you make a request to a route and that route passes the control over to a controller/function and from there you show/load the view. So it's not a tutorial site and it's also not possible to explain about MVC here, you should read about it and for Laravel, it's best place to understand the basics on it's documentation, well explained with examples, anyways.
In case of Laravel, you should create a controller/class or an anonymous function in your apps/routes.php file and show the view from one of those. Just follow the given instruction step by step.
Using a Class:
To create a route to your Home Controller you should add this code in your app/routes.php
// This will call "showWelcome" method in your "HomeController" class
Route::any('/', array( 'as' => 'home', 'uses' => 'HomeController#showWelcome' ));
Then create the HomeController controller/class (create a file in your controllers folder and save this file using HomeController.php as it's name) then paste the code given below
class HomeController extends BaseController {
public function showWelcome()
{
// whatever you do, do it here
// prepare some data to use in the view (optional)
$data['page_title'] = 'Home Page';
// finally load the view
return View::make('home', $data);
}
}
If you have {{ $title }} in your home.blade.php then it'll print Home Page. So, to use a view you need a controller or an anonymous function and load the view from the controller/function.
Using an anonymous function:
Also, you can use an anonymous function instead of a controller/class to show the view from directly your route, i.e.
Route::any('/', function(){
// return View::make('home');
// or this
$data['page_title'] = 'Home Page'; // (optional)
return View::make('home', $data);
});
Using this approach, whenever you make a request to the home page, Laravel will call the anonymous function given in/as route's callback and from there you show your view.
Make sure to extend the the master/main layout in sub view (home):
Also, remember that, you have following at the first line of your home.blade.php file
#extends('layouts.layout')
It looks confusing, you may rename the main layout (layout.blade.php) to master.blade.php and use following in your home.blade.php instead
#extends('layouts.master')
Read the doc/understand basics:
You should read Laravel's documentation properly, (check templates to understand blade templating) and also read some MVC examples, that may help you too understand the basics of an MVC framework (you may find more by googling) and some good posts about MVC on SO.
Check it routing in Laravel.
You need to use route file and controllers
Create needed function in your Controller file and create a template file for example
class UserController extends BaseController {
/**
* Show the profile for the given user.
*/
public function showProfile($id)
{
$user = User::find($id);
return View::make('user.profile', array('user' => $user));
}
}
you need to create view file views/user/profile.blade.php
View::make('user.profile', array('user' => $user)) == views/user/profile.blade.php
And you should read it http://laravel.com/docs/responses and also this http://laravel.com/docs/quick#creating-a-view