ファイルを正確に作成する場所、書き込む場所、およびその特性で宣言された関数の使用方法の例が必要です。私はLaravelフレームワーク5.4.18Laravel 5.4.18で形質を使用するには?
-Iを使用は枠組みの中で任意のフォルダを変更していない、それはどうもありがとうございましすでにから
をcorresponds-ところ、すべてがあります。
ファイルを正確に作成する場所、書き込む場所、およびその特性で宣言された関数の使用方法の例が必要です。私はLaravelフレームワーク5.4.18Laravel 5.4.18で形質を使用するには?
-Iを使用は枠組みの中で任意のフォルダを変更していない、それはどうもありがとうございましすでにから
をcorresponds-ところ、すべてがあります。
私はBrandsTrait.php
と呼ばれる形質に私のHttp
ディレクトリにトレイトディレクトリを作成したいと、それを使用しています。ここ
use App\Http\Traits\BrandsTrait;
class YourController extends Controller {
use BrandsTrait;
public function addProduct() {
//$brands = Brand::all();
// $brands = $this->BrandsTrait(); // this is wrong
$brands = $this->brandsAll();
}
}
は私のBrandsTrait.php
<?php
namespace App\Http\Traits;
use App\Brand;
trait BrandsTrait {
public function brandsAll() {
// Get all the brands from the Brands Table.
$brands = Brand::all();
return $brands;
}
}
注意です:普通の関数のように、ある特定のnamespace
に書かれているように、traits
も同様
形質はフレームワーク機能ではなく、言語機能です。だからhttp://php.net/manual/en/language.oop5.traits.phpはあなたが必要とするすべてのものでなければなりません。 –
@MateuszSipはい私は知っています。しかし、どこにファイルを置くのですか? – emi
あなたは[namespaces](http://php.net/manual/en/language.namespaces.php)を認識していますか? あなたの形質をインポートして使用するだけです。 –