Laravel 5.4 –创建立面时出错

时间:2018-08-10 09:31:36

标签: php laravel laravel-5 laravel-5.4

这是我创建助手(App \ Helpers \ Settings.php)的方式

namespace App\Helpers;

use Illuminate\Database\Eloquent\Model;

class Settings {

    protected $settings = [];

    public function __construct() {

        $this->settings['AppName'] = 'Test';
    }

    /**
     * Fetch all values
     *
     * @return mixed
     */
    public function getAll () {
        return $this->settings;
    }
}

创建外观(App \ Helpers \ Facades \ SettingsFacade.php)

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Settings extends Facade {

    protected static function getFacadeAccessor() {
        return 'Settings';
    }
}

创建服务提供商(App \ Providers \ SettingsServiceProvider.php)

namespace App\Providers;

use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;

class SettingsServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot() {
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        App::bind( 'Settings', function () {
            return new \App\Helpers\Settings;
        });
    }  */
}

注册提供商(App \ Providers \ SettingsServiceProvider :: class)

创建别名:'Settings' => App\Facades\Settings::class

运行composer dump-autoload

尝试使用立面Settings::getAll();

获取错误类'App\Http\Controllers\Settings' not found

无法弄清楚为什么我不能创建立面并得到该错误

1 个答案:

答案 0 :(得分:0)

尝试这个。

App \ Helpers \ Settings.php

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChatRoom">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomNavigationView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

App / Http / Controllers / XyzController.php

namespace App\Helpers;
use Illuminate\Database\Eloquent\Model;
    class Settings {

        protected $settings = [];

        public function __construct() {

            $this->settings['AppName'] = 'Test';
        }

        /**
         * Fetch all values
         *
         * @return mixed
         */
        public function getAll () {
            return $this->settings;
        }
    }

web.php

use Facades\App\Settings;
class XyzController extends Controller
{
     public function showView()
     {
         return Settings::getAll();
     }
}

使用laravel Real time facades