简体   繁体   English

将变量传递到视图中

[英]Passing variables into views

I am a new learner of Laravel,and currently I am playing with views. 我是Laravel的新手,目前我在玩观点游戏。 However,after passing a variable in my view,I get this exception. 但是,在我的视图中传递了变量后,出现了此异常。 Basically it's saying that the route doesn't exist. 基本上说这条路线不存在。

NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 821
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in    
CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'),    
array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54

So here is what I am doing 所以这就是我在做什么

routes.php routes.php

Route::get('show_comment/{$id}','CommentsController@showComment');

CommentsController.php CommentsController.php

class CommentsController extends Controller
{

public function index()
{

}
/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
    return "inside Create method";
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    return "Show method:" .$id;
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
  public function update(Request $request, $id)
  {
    //
  }


public function destroy($id)
{
    //

}

public function contact(){
    return view('contacts');
}


public function showComment($id){
    return view('show_comment') -> with('id',$id);
  }

}

And finally I have my simple view. 最后,我有一个简单的看法。

<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>

<link href="https://fonts.googleapis.com/css?family=Lato:100"    
rel="stylesheet" type="text/css">

</head>
<body>
<div class="container">
<h1/>Show comment {{$id}}}</h1>
</div>
</body>
</html>

What am I missing? 我想念什么?

Thanks, 谢谢,

Theo. 西奥

As @Xatenev said in the comments, it should just be a case of removing $ in the route so changing: 就像@Xatenev在评论中说的,应该只是删除路由中的$一种情况,然后进行更改:

Route::get('show_comment/{$id}','CommentsController@showComment');

to: 至:

Route::get('show_comment/{id}','CommentsController@showComment');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM