Create a Facade Laravel 5.4

1

I am new using laravel and what I want to do to incorporate into my project a class of my own that I have developed.

I tried to follow the steps of this link but it does not work out, what I done so far is the following:

1.) Create the Alias config \ app.php:

'Alert' => 'Facades\App\Alert',

2.) Create the File that stores the class in the App folder (app \ Alert.php ) This is the content of the class:

<?php  

    namespace App;

    class Alert 
    {
        public function message($message, $type)
        {
            return $message;
        }

        public function render()
        {
            return 'render';
        }
    }

?>

3.) Create the AlertController Driver as follows:

 php artisan make:controller AlertController

Add the facade to the controller:

<?php
    namespace Sistem_Administrador\Http\Controllers;

    use Illuminate\Http\Request;
    use Facades\App\Alert; //<-Facade agregado

    class AlertController extends Controller
    {
        public function index()
        {
            return Alert::message('Mensaje1','Typo1');
        }
    }

4.) Create the Route in the routes \ web.php file

Route::get('/Alert','AlertController@index');

I do not know if I need a step but as far as I saw on the page that I mentioned before, that would be all, the laravel version I have is 5.4

The error that returns to me says that Class App \ Alert does not exist, as seen in the following image:

    
asked by Andrés 05.07.2017 в 22:23
source

1 answer

1

I was able to solve my problem and I will leave the solution in case someone presents the same situation:

The steps I took were correct, the only thing I had to do was look for "autoload" inside the composer.json file and add my class, if it does not exist, you have to add it

"autoload": {
            "classmap": [
                "database",
                "app/Alert.php" <- La linea que agregue
            ]
}

After adding it to the console, I should only execute the following command:

composer dump-autoload -o

And that's it, it's already running smoothly, Thanks anyway to those who bothered to review :).

    
answered by 06.07.2017 в 00:24