Php 在视图中包括控制器laravel 5 /** * Show the profile for the given user. * * @param int $id * @return Response */ public function index(){ //here comes a whole list with what i've done $results = DB::table('projects')->get(); //return $results; $data = array(); foreach ($results as $key => $result) { $data[] = $result; } return view('portfolio.portfolio')->with('data', $data); } public function getProject($portfolio_url){ //this gets the project thats clicked $results = DB::select('select * from projects where portfolio_url = ?', array($portfolio_url)); return view('portfolio.single')->with('data', $results['0']); } } class menuController extends Controller { /** * Show the profile for the given user. * * @param int $id * @return Response */ // public function __construct($table){ // $results = DB::table($table)->get(); // return view('menu')->with('menuknoppen', $results); // } public function index(){ $results = DB::table('navigation')->get(); return view('menu')->with('menuknoppen', $results); } }
我是拉威尔的新手,我被卡住了 我的问题是我想要两个有动态数据的部分(导航、内容) 这里有一些代码 主叶片Php 在视图中包括控制器laravel 5 /** * Show the profile for the given user. * * @param int $id * @return Response */ public function index(){ //here comes a whole list with what i've done $results = DB::table('projects')->get(); //return $results; $data = array(); foreach ($results as $key => $result) { $data[] = $result; } return view('portfolio.portfolio')->with('data', $data); } public function getProject($portfolio_url){ //this gets the project thats clicked $results = DB::select('select * from projects where portfolio_url = ?', array($portfolio_url)); return view('portfolio.single')->with('data', $results['0']); } } class menuController extends Controller { /** * Show the profile for the given user. * * @param int $id * @return Response */ // public function __construct($table){ // $results = DB::table($table)->get(); // return view('menu')->with('menuknoppen', $results); // } public function index(){ $results = DB::table('navigation')->get(); return view('menu')->with('menuknoppen', $results); } },php,laravel-5,Php,Laravel 5,我是拉威尔的新手,我被卡住了 我的问题是我想要两个有动态数据的部分(导航、内容) 这里有一些代码 主叶片 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Portfolio</title> </head> <body> <div class="navigat
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Portfolio</title>
</head>
<body>
<div class="navigation">
@yield('menu')
</div>
<div class="content">
@yield('content')
</div>
</body>
</html>
我的导航控制器
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function index(){
//here comes a whole list with what i've done
$results = DB::table('projects')->get();
//return $results;
$data = array();
foreach ($results as $key => $result) {
$data[] = $result;
}
return view('portfolio.portfolio')->with('data', $data);
}
public function getProject($portfolio_url){
//this gets the project thats clicked
$results = DB::select('select * from projects where portfolio_url = ?', array($portfolio_url));
return view('portfolio.single')->with('data', $results['0']);
}
}
class menuController extends Controller {
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
// public function __construct($table){
// $results = DB::table($table)->get();
// return view('menu')->with('menuknoppen', $results);
// }
public function index(){
$results = DB::table('navigation')->get();
return view('menu')->with('menuknoppen', $results);
}
}
您的主刀片应该是:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Portfolio</title>
</head>
<body>
<div class="navigation">
@include('menu');
</div>
<div class="content">
@yield('content')
</div>
</body>
</html>
@extends('main')
@section('content')
@foreach($data as $portfolio)
<a href="portfolio/{!!$portfolio->portfolio_url!!}"><img src='{{ URL::asset("images/$portfolio->picture.jpg") }}'/></a>
@endforeach
@stop
//Don't use extends here
@foreach($menuknoppen as $menuknop)
<a href='{{ URL::to("$menuknop->menu_url") }}'>{{$menuknop->menutitle}}</a>
@endforeach
您混合了@yield、@include和@extend的概念 @yield为您提供了替换的位置,因此当您在其他视图中调用@extend时,您可以在您扩展的视图中重用模板,并使用@yield替换零件 @include意味着这部分代码总是被它定义的视图替换 因此,当你设计一个网页时,你需要确保什么是“总是被调用的”(使用@include)和什么可以被替换(使用@yield)
作为对aldrin27工作代码的辅助解释,我希望这能让您在blade模板上的思路更清晰,它震撼了!:D您想将导航添加到您的投资组合中吗?那么您的实际问题是什么?对不起,我没有问这个问题更新导航在您的文件树中的位置?例如:视图/布局/导航。您希望在您的投资组合中包含导航,我说得对吗?现在的问题是如何在主刀片中添加导航视图?我刚刚完成了。我把它包括进去了。试着修改你的代码。真的很抱歉没看到!现在我又遇到了同样的错误,未定义变量:menuknopen。当您包含一个视图时,它是否执行控制器?因为我的导航是从数据库进行的,我能看到你的控制器吗?还有你如何称呼这个
$menuknopen
我添加了控制器
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function index(){
//here comes a whole list with what i've done
$results = DB::table('projects')->get();
//return $results;
$data = array();
foreach ($results as $key => $result) {
$data[] = $result;
}
return view('portfolio.portfolio')->with('data', $data);
}
public function getProject($portfolio_url){
//this gets the project thats clicked
$results = DB::select('select * from projects where portfolio_url = ?', array($portfolio_url));
return view('portfolio.single')->with('data', $results['0']);
}
}
class menuController extends Controller {
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
// public function __construct($table){
// $results = DB::table($table)->get();
// return view('menu')->with('menuknoppen', $results);
// }
public function index(){
$results = DB::table('navigation')->get();
return view('menu')->with('menuknoppen', $results);
}
}