2016-07-29 71 views
0

我有一个小的WebSockets聊天写的,PHP的一部分仅仅是2个文件,server.phpChat.php,他们都是一个bin文件夹内,并取决于ratchet和其他一些图书馆,我通过作曲家下载到laravel安装。如何正确添加外部PHP文件中Laravel 5

server.php

require __DIR__.'/../vendor/autoload.php'; 
require 'Chat.php'; 

use Ratchet\Server\IoServer; 
use Ratchet\http\HttpServer; 
use Ratchet\WebSocket\WsServer; 

$server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 8080); 

$server->run(); 

Chat.php

use Ratchet\MessageComponentInterface; 
use Ratchet\ConnectionInterface; 

class Chat implements MessageComponentInterface { 

    protected $clients; 

    function __construct() { 
     $this->clients = new \SplObjectStorage; 
    } 

    public function onOpen(ConnectionInterface $conn) 
    { 
     $this->clients->attach($conn); 
    } 

    public function onMessage(ConnectionInterface $conn, $msg) 
    { 
     foreach ($this->clients as $client) 
     { 
      if ($client !== $conn) { 
       $client->send($msg); 
      } 
     } 
    } 

    public function onClose(ConnectionInterface $conn) 
    { 
     $this->clients->detach($conn); 
    } 

    public function onError(ConnectionInterface $conn, \Exception $e) 
    { 
     echo 'the following error occured: ' . $e->getMessage(); 
     $conn->close(); 
    } 

} 

现在,我有bin文件夹中的laravel根里面,所以我能够启动服务器后server.php期待对于供应商一级的依赖关系,但我想要做的是在这些文件中使用所有的laravel好东西,特别是在Chat.php之内。

因此,现在例如,如果我写use DBChat.php它给出了一个错误(我明白,它无法知道laravel),所以我的问题是如何包含此bin文件夹及其文件,以便我可以使用它们中的所有laravel好东西?

回答

0

您不需要手动加载供应商/ autoload.php,因为laravel可以为您做到这一点。 首先你必须在YourLaravelRoot/app目录下创建文件夹(让我们将其命名为Services)。然后移动chat.php到ChatService.php(将类名更改为ChatService)或任何合适的名称(reccomanded以xxxxService结尾,以便识别),并将其命名为namespace App\Services;(假设您的应用程序名称为App).Namespacing正确是非常重要的,否则你必须通过composer.json手动加载它。然后创建一个artisan命令并将server.php的内容移动到命令内部的handle方法中(我们将其命名为ServerCommand.php)。添加use App\Services\ChatService as Chat;。在app/console上注册Kernal.php中的命令就是这样。现在,你应该能够访问任何laravel门面内ChatService

摘要:

YourLaravelProject

-app

--console

Kernal.php

<?php 

namespace App\Console; 

use Illuminate\Console\Scheduling\Schedule; 
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; 

class Kernel extends ConsoleKernel 
{ 
    /** 
    * The Artisan commands provided by your application. 
    * 
    * @var array 
    */ 
    protected $commands = [ 
     Commands\ServerCommand::class, 
    ]; 

    /** 
    * Define the application's command schedule. 
    * 
    * @param \Illuminate\Console\Scheduling\Schedule $schedule 
    * @return void 
    */ 
    protected function schedule(Schedule $schedule) 
    { 
     // $schedule->command('inspire') 
     //   ->hourly(); 
    } 
} 

---命令

---- ServerCommand.php

<?php 

namespace App\Console\Commands; 

use Illuminate\Console\Command; 
use Ratchet\Server\IoServer; 
use Ratchet\Http\HttpServer; 
use Ratchet\WebSocket\WsServer; 
use App\Services\ChatService as Chat; 

class ServerCommand extends Command 
{ 
    /** 
    * The name and signature of the console command. 
    * 
    * @var string 
    */ 
    protected $signature = 'server:run'; 

    /** 
    * The console command description. 
    * 
    * @var string 
    */ 
    protected $description = 'Command description'; 

    /** 
    * Create a new command instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    /** 
    * Execute the console command. 
    * 
    * @return mixed 
    */ 
    public function handle() 
    { 
     $server = IoServer::factory(new HttpServer(new WsServer(new Chat)), 8080); 
     $server->run(); 
    } 
} 

--Services

--- ChatService。PHP

<?php 
namespace App\Services; 

use Ratchet\MessageComponentInterface; 
use Ratchet\ConnectionInterface; 
/** 
* 
*/ 
class ChatService implements MessageComponentInterface { 
{ 
    protected $clients; 

    function __construct() { 
     $this->clients = new \SplObjectStorage; 
    } 

    public function onOpen(ConnectionInterface $conn) 
    { 
     $this->clients->attach($conn); 
    } 

    public function onMessage(ConnectionInterface $conn, $msg) 
    { 
     foreach ($this->clients as $client) 
     { 
      if ($client !== $conn) { 
       $client->send($msg); 
      } 
     } 
    } 

    public function onClose(ConnectionInterface $conn) 
    { 
     $this->clients->detach($conn); 
    } 

    public function onError(ConnectionInterface $conn, \Exception $e) 
    { 
     echo 'the following error occured: ' . $e->getMessage(); 
     $conn->close(); 
    } 
} 

执行命令php artisan server:run

+0

我猜你的意思是应用程序/ HTTP /控制器,反正我做了一个'ChatController'这个命名空间:'命名空间应用程序\ HTTP \控制器;'和我做了'聊天服务器,php'在根文件夹中使用这个声明:'使用App \ Http \ Controllers \ ChatController;',但是当我从控制台运行它时,我得到这个:'Class'App \ Http \ Controllers \ ChatController'not fou nd in C:\ wamp \ www \ laraveltesting \ chatserver.php:13 ' – user6562256

+0

要运行你的服务器,你需要调用server.php。所以它应该在一个控制器中。而chat.php只是一个帮助类来运行服务器,所以它是server.php的一个服务。这就是我提出上述结构的原因。 –

+0

查看我的更新回答 –