Reputation: 3841
I want to reuse the same return View::make('pages.home');
template view for my UserController
but I am not sure how to connect the view to that controller.
I am assuming I need to put something in my index
of my controller but I am not exactly sure what as this is my first time with Laravel.
Here is my current code:
Routes.php
<?php
Route::get('/home', function () {
return View::make('pages.home');
});
Route::get('/', 'UserController@index');
Route::get('user/{id}', 'UserController@show');
Route::get('/foo', function () {
return 'taobao';
});
UserController.php //simplified
public function index()
{
$this->oauth();
return view('user', ['name' => 'James']);
}
Upvotes: 0
Views: 3298
Reputation: 2474
If you need to reuse the same return View::make('pages.home');
or home view in your UserController you can call it as
return view('home', ['name' => 'James']);
or you need to include home view in users view then you can do
@extends('home') or @include('home')
Upvotes: 1
Reputation: 21681
I think you should try this:
Added below line in your user.blade.php
@include('pages.home')
Upvotes: 1
Reputation: 1816
You don't need to put anything specific to the index()
method of your Controller. Think of a Controller as the Brains that ehemmm.. "control" what's going to happen, either a long query to the database, calculating some operations, returning a view, or a combination of all.
In your code above Route::get('/', 'UserController@index');
is actually binding the /
"address" or route to the method index()
of your UserController
.
In regards to what you asked, you need to split up your pages.home
view so it can be reusable. I recommend you reading the templates documentation.
In short, you could have the following page, called base.blade.php
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page</title>
<!-- Bootstrap includes -->
<!-- JS Includes -->
</head>
<body>
<nav>
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
</ul>
</nav>
<div class="content">
@yield('content')
</div>
</body>
</html>
Then, we can create 2 more pages.
users.blade.php
:
@extends('layouts.base')
@section('content')
<div> Hello Users!</div>
@endsection
admins.blade.php
:
@extends('layouts.base')
@section('content')
<div> Hello Admins!</div>
@endsection
Those two blade templates will inject the HTML inside their correspondent sections, in this case content, into the template base.blade.php
whenever they are called. That way, you can "reuse" a view, which is very efficient since changing base.blade.php
will change all the views that "inject" or inherit from it.
Now, like you said, in order to "connect" the view to a controller, you simply need to return a view from within a method in that controller. For example:
MyController.php
:
MyController extends Controller{
...
public function index()
{
// Fancy stuff you might wanna do...
// Note this will pull the view from within the layout folder
return view('users');
}
}
Of course, you need to have a route that is paired with that Controller and that method, which in this case you do.
I hope this helps!
Cheers!
Upvotes: 1
Reputation: 2010
You don't exactly need to connect your view to the controller in Laravel. You just need to put your view to the specific location (Either default or intended).
After you have made your view and stored in the view directory, You can call it inside any controller. There is no strict connection between views and controller in MVC.
You just need to return return View::make('pages.home');
from any controller action you want and reuse it as many times. Or, you can use the helper instead return view('pages.home');
Hope that is what you seek.
Upvotes: 1
Reputation: 314
You can try this
public function index()
{
$this->oauth();
$user = ['name' => 'James'];
return view('user', compact('user'));
}
Now you can call $user
in user view.
Example (using blade): {{ $user['name'] }}
, the output would be James
Upvotes: 1