2016-12-21 147 views
-1

Hej!我的问题是下一个。我开始使用Laravel 5.3。我怎样才能做到这一点class在Laravel:Laravel 5.3 class/routes

class Vehicle 
{ 
    public $vehicletype; 

    function invt($vehicletype){ 
     $this->vehicletype=$vehicletype; 
    } 
    function outvt(){ 
     return $this->vehicletype; 
    } 
} 

我已经有AJAX .post,路线:

Route::get('/ajax-vehicletype',function(){ 
    $vehicletypevalue=Input::get('vehicletype'); 
    Vehicle::invt($vehicletypevalue); 
}); 

我得到错误:

Non-static method App\Vehicle::invt() should not be called statically, assuming $this from incompatible context 

感谢。

+2

无关,与Laravel,学习OOP静态和非静态方法调用之间的区别:http://php.net/manual/en/language。 oop5.static.php – Devon

+0

你没有定义函数是公共的,私有的,保护的还是静态的 – Phorce

回答

2

您可以定义控制器,然后在该控制器中定义函数,并将其传递给您的路由参数。

class Vehicle extends Controller{ 
     public $vehicletype; 

     function invt($vehicletype){ 
      $this->vehicletype=$vehicletype; 
     } 
    function outvt(){ 
     ........... 
     } 
} 

在你的路线文件,你可以定义路由为

Route::get('/ajax-vehicletype/{vehicletype}','[email protected]');