laravel send variable to login route - laravel

I am using a different app.blade.php than the one provided with the default auth system in laravel. In the one I have created, I need to give a variable each time so that it show where we are in the code. For example, for my routes, I use:
Route::get('/', ['as' => 'home', function () {
$position = ['main_title'=>'Home','second_title'=>'',
'urls'=>
[
['name'=>'home','url'=>'#']
]
];
return view('home')->with('position',$position);
}]);
And in my app.blade.php, I can use this $position to print the main title etc.
Now the problem comes when I try to access login because $position is not set. How can I achieve that in Laravel so that the auth system will use my new blade system?
Thanks.

The auth views use the layout file app.blade.php located in resources/views/layouts. This is done by the code #extends('layouts.app') at top of all the views, which means that the auth views extend this layout.
To change the layout in all the auth views, manually replace the #extends('layouts.app') part at top with the new layout file you have created. For example if your new custom layout file is resources/views/base.blade.php, then replace the default extends to #extends('base').

If you want to use a variable in different (all) views, you may use ViewComposer

Related

File extension for Laravel Blade as an 'index' page

My 'homepage' for a Laravel install works great, which is: welcome.blade.php however, my question is - how do I get other basic 'static' pages to work correctly.
My preferred site top level navigation has for example, 'about us', so what I'd like to do is create a directory called 'about us' and then place a similar page as blade.index.php in there but it throws an error.
So, my question is - how do I name the file within a directory within the web application.
Thanks
You've got it flipped. blade always goes in the middle.
index.blade.php
return view('index');
/about/index.blade.php
return view('about.index');
You will need put your view file inside static folder of your choice and make sure your filename should contain blade in the middle for ex: "about.blade.php" and after that you will need to return that file with folder_name/file_name.
return view('Folder_name/File_name');
Try above code you are good to go.

In Laravel how to add language part in all anchor links?

I am using blade template and put simple url like
About us
Now I change language like
mydomain.com/hn/
then how i can add language part in all url like
mydomain.com/hn/aboutus
Take a look at Laravel localization. It may be useful to you.
Laravel Localization
Try to create a link like this
About us
and then in routes/web.php make a route for it like this
Route::get('aboutus/hn', function () {
return 'About us page';
});

How to pass value to layout without passing value from all functions in Laravel?

In my project I am using a layout file and other view files. In my layout, there is a place to display users data. But I using this layout for different pages, so when I uses this layout for each pages, I need to pass user details from corresponding functions to view page. Is there any other short cut to pass user data only once, to layout directly??
Can anyone please reply??
Use Laravel View Composers
Official documentation
Here is an example :
'layout' is your view
View::composer('layout', function($view)
{
$view->with('count', User::count());
});
Put this into your "/app/start/global.php" file

Laravel 4 passing data to #include

I have a view called admin.users. In that view I include header and footer using #include directive. I write all my views-including stuff in routes.php, so for admin.users it is:
Route::get('users', function() {
// ...
return View::make('admin.users')->with('num', $usersNum);
}
And in users.blade.php:
#include('admin.partials.header')
// ....
#include('admin.partials.footer')
Is it possible to pass "users num" to header view in order to show that variable? And is it a good practice the way I'm combining views, because I read about controller layouts but actually I decided to have only rest controllers while I include view only in routes.php (like load static markup and after that communicate with server by ajax)
Have a look at 'ViewComposers' (http://laravel.com/docs/responses#view-composers). This is a great way to share data with your views and keep your routes file clean.
In global.php (or any other place really) add:
View::composer('admin.users', function($view)
{
// Do your $usersNum logic here
...
$view->with('num', $usersNum);
});
If at some point you want this data to be available in admin.dashboard as well, just rewrite to:
View::composer(array('admin.users', 'admin.dashboard'), function($view)
As I said in my comment (and what seems to have fixed your issue) you should create an admin.template view with your header and footer in it, and start your views with #extends('admin.template').

CI: Use view/controller in another included view?

I'm new to CI & PHP.
I have an auth library included, and works great stand-alone.
I simply want to have the login form load as a view inside another view...is that weird?:
I'm quasi-templating:
index:
$this->load->view('head_content');
$this->load->view('stuff');
$this->load->view('footer');
Inside the stuff view:
<stuff></>
$this->load->view('login_view');
<morestuff></>
I just want the login form to show up on the front page, and then tie into the auth system...
You have to load the login view in the controller, and then pass the data to the stuff view.
In the controller:
$this->load->view('head_content');
// the line below will save the output of the login view to $data['login']
// instead of outputting to the screen
$data['login'] = $this->load->view('login_view', '', TRUE);
$this->load->view('stuff');
$this->load->view('footer');
In the stuff view:
<stuff>
<?php echo $login; ?>
<morestuff>
In the page Views, at the bottom, check out the section Returning views as data
I've just recommended this but I'm going to recommend it again:
http://codeigniter.com/forums/viewthread/77279/
Django-like template inheritance helper for CodeIgniter.
I use this on ALL of my CI projects and it makes things like this stupidly easy.

Resources