dsf4354353452 2014-05-08 22:20
浏览 13
已采纳

Laravel:连接服务

So I've implemented a service structure that a stackoverflow user suggested to me in this post. I've got it mostly working but I'm running into the error:

Class 'App\Services\Mailer\Facades\Mailer' not found

My service folder structure looks like this:

app
|   App
|   |   Services
|   |   |   Mailer
|   |   |   |   Mailer.php
|   |   |   |   MailerFacade.php
|   |   |   |   MailerServiceProvider.php

Each of the files in the Mailer directory are namespaced as:

<?php namespace App\Services\Mailer;

Except for the Facade which is namespaced as the following per the example in this blog:

<?php namespace App\Services\Mailer\Facades; 

I put a test method in my Mailer.php file's Mailer class:

<?php namespace App\Services\Mailer;
//base service class

class Mailer {

  public function sayHi(){
    return "hello!";
  }      
}

I then created the facade:

<?php namespace App\Services\Mailer\Facades; 
// Facade for Mailer

use Illuminate\Support\Facades\Facade;

class Mailer extends Facade {

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

Then I created the Service Provider to hook them all together:

<?php namespace App\Services\Mailer;
// Mailer's service provider

use Illuminate\Support\ServiceProvider;

class MailerServiceProvider extends ServiceProvider {
    public function register(){

        $this->app['mailer'] = $this->app->share( function ($app){
            return new App\Services\Mailer\Mailer;
        });

        $this->app->booting( function (){
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('Mailer', 'App\Services\Mailer\Facades\Mailer');
        });
    }
} 

THe loader for the facade is pointing to the correct namespace, 'App\Services\Mailer\Facades\Mailer', but when I try calling the method in one of my controllers like so:

public function showMe(){
    return Mailer::sayHi();
}

I get the message I noted at the top.

I tried putting the facade into a subfolder of the Mailer directory called Facade so that the namespace and folder structure matched exactly, but I got the same error.

I read and re-read the example, the original stackoverflow post, the composer documentation on psr-4 to make sure I wasn't referencing anything incorrectly, and I can't seem to figure it out.

Can anyone point me in the right direction?

EDIT:

So I took a step back and created a vanilla laravel project and tried to add in a service the same way I'm trying in my main project. It's running into a similar error.

Here's a screen shot of every file involved:

enter image description here

Process:

  • I created my new laravel project
  • I created my App/Services/Greetings directory structure
  • I added my psr-4 load path to my composer.json file
  • I created my underlying class Greetings
  • I Created a GreetingsFacade class that returns the string 'greetings' for the `getFacadeAccessor method
  • I created a Greetings Service Provider class GreetingsServiceProvider and added the register method with it's commands
  • I added my service provider to the app/config/app.php file in the providers array enter image description here
  • I added a route that returns a closure that uses my new Greetings service
  • I then ran composer dump-autoload and loaded my route

When I load my page I get the error "Class 'App\Services\Greetings\App\Services\Greetings\Greetings' not found" :(

  • 写回答

2条回答 默认 最新

  • doujuanju3076 2014-05-09 12:17
    关注

    According to your directory structure:

    app
    |   App
    |   |   Services
    |   |   |   Mailer
    |   |   |   |   Mailer.php
    |   |   |   |   MailerFacade.php
    |   |   |   |   MailerServiceProvider.php
    

    Your classes namespaces must be:

    Service

    <?php namespace App\Services\Mailer; 
    
    class Mailer {}
    

    Service Provider

    <?php namespace App\Services\Mailer; 
    
    class MailerServiceProvider {}
    

    Facade

    <?php namespace App\Services\Mailer; 
    
    class MailerFacade {}
    

    And you autoloader:

    "autoload": {
        "psr-4": {
            "App\\": "app/App"
        },
    },
    

    For it to work the way you are trying to do, your directory structure would have to be

    app
    |   App
    |   |   Services
    |   |   |   Mailer
    |   |   |   ├── Facades
    |   |   |   |   └── Facade.php
    |   |   |   |   Mailer.php
    |   |   |   |   MailerServiceProvider.php
    

    EDIT:

    In your Greetings namespace, change from:

    return new App\Services\Greetings\Greetings;
    

    to

    return new Greetings;
    

    And it should work because they are in the same namespace. For an easy reading of the code, add

    use App\Services\Greetings\Greetings;
    

    In the top of your .php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度