我要做的只是向控制器添加自定义方法,以便我可以在控制器内部的其他方法中重用它们。例如。:
class SampleController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
generateCode();
sendEmail();
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
/**
* Custom Functions
*/
function sendEmail()
{
}
function generateCode()
{
}
}
在这里,我只想能够调用它sendEmail()
并generateCode()
在我的索引方法中,但是我一直在获取未定义的函数。
它们不是全局函数,因此您不能那样使用它们。它们是具有范围的类方法。请注意我为您所做的更改:
<?php
class SampleController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$this->generateCode();
$this->sendEmail();
}
// . . .
/**
* Custom Functions
*/
private function sendEmail()
{
}
private function generateCode()
{
}
}
您确实应该编写一些OOP PHP教程,或者阅读一些书籍,或者上一门课程。这是非常基本的东西,您不需要在堆栈溢出时询问。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句