laravel public 不同控制器的变量
laravel public variable for different controllers
我想设置一个 public 变量,以便我可以在不同的控制器中使用它。我试过了,但没有成功。
class HomeController extends Controller
{
public $TravelId;
public function __construct()
{
$this->TravelId=0;
}
}
然后我在另一个控制器中使用了相同的变量
class HomeController extends Controller
{
public function index($cus_id)
{
//unique key for each user
$this->TravelId = $cus_id;
}
}
默认情况下,您所有的控制器都会扩展 App\Http\Controllers\Controller
class,因此您可以在此 class 上添加一个 属性 并在您的所有控制器上访问它:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* Your static property
*/
public static $travelId;
}
然后你可以在另一个控制器上访问它:
self::$travelId = $cus_id;
我想设置一个 public 变量,以便我可以在不同的控制器中使用它。我试过了,但没有成功。
class HomeController extends Controller
{
public $TravelId;
public function __construct()
{
$this->TravelId=0;
}
}
然后我在另一个控制器中使用了相同的变量
class HomeController extends Controller
{
public function index($cus_id)
{
//unique key for each user
$this->TravelId = $cus_id;
}
}
默认情况下,您所有的控制器都会扩展 App\Http\Controllers\Controller
class,因此您可以在此 class 上添加一个 属性 并在您的所有控制器上访问它:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* Your static property
*/
public static $travelId;
}
然后你可以在另一个控制器上访问它:
self::$travelId = $cus_id;