1.创建普通控制器
$ php artisan make:controller WelcomeController
使用函数响应结果
class WelcomeController extends Controller
{
public function index()
{
return view('welcome.index');
}
public function about()
{
return view('welcome.about');
}
}
2.创建资源控制器
$ php artisan make:controller IssuesController -r
控制器自带7个资源方法
class IssuesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* 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)
{
//
}
/**
* 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)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
3.控制器的函数返回值
a.直接返回视图
return view('issues.create'); // create函数
b.返回视图并带参数
return view('issues.index')->with('issues', $issues);
return view('issues.edit')->with('issue', $issue);
c.返回视图并带组合参数
return view('issues.show', compact('issue', 'comments'));
d.返回-路由重定向
return redirect('/'); // 往往store或destroy后,都会路由重定向
e.返回-路由重向定到有参数路由
return redirect(route('issues.show', $id));
网友评论