Laravel 5.2 and Ratchet Class not found - php

I am using Ratchet for websockets. It works in general, but I want to use inside my ExampleController Laravels Auth. It should be easy but this does not work:
<?php namespace Annotation\Http\Controllers;
use Auth;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ExampleController extends Controller implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) {
echo Auth::id();
//etc.
echo "New Connection! ({$conn->resourceId})";
}
}
I always get a Class Auth not found exception, when I init my Controller in the websocket-server.php (located in the root dir of laravel) file below:
<?php
require __DIR__.'/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Annotation\Http\Controllers\CollaborativeController;
$server = IoServer::factory(
new ExampleController(),
8080
);
$server->run();
If I use my ExampleController as a usual controller with a route, the Auth class will be found. (I am also not able to use the auth helper or anything related with laravel)
Why this happens? Because Laravel is not initialized yet or do I need to add path?

The auth function returns an authenticator instance. You may use it instead of the Auth facade for convenience:
echo auth()->user()->id;

Add the following lines to your server.php if you want to load middlewares.
Auth is a middleware and it's not initialised and loaded.
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php'; (set a proper path to your bootstrap)

Related

Required "token" not supplied in config and could not find fallback environment variable "TELEGRAM_BOT_TOKEN"

I am using PHP 7.4.1 and Laravel Framework 6.20.16.
I am trying to implement the following library: telegram-bot-sdk and the following version "irazasyed/telegram-bot-sdk": "^2.0",
After installing the sdk and getting my private token from telegram's #botfather. I am trying to use the sdk.
I created a route and a controller:
route
Route::get('telegramHello', 'TelegramController#getHello');
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\Api as Telegram;
use App\Helpers\TelegramResponse as Response;
class TelegramController extends Controller
{
public function getHello() {
$api = new Telegram(); // ----> HERE I GET THE ERROR
$response = $api->getMe();
return Response::handleResponse($response);
}
//...
When opening my route I get the following exception:
The thing I do not understand is that I have created the config telegram.php and loading my correct token from my .env file:
In my .env file it looks like the following:
Any suggestions what I am doing wrong?
I appreciate your replies!
Use Facade, not original API class. Your config is correct, you just using wrong class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\Laravel\Facades\Telegram;
use App\Helpers\TelegramResponse as Response;
class TelegramController extends Controller
{
public function getHello() {
$response = Telegram::getMe();
return Response::handleResponse($response);
}
//...
Also i may recommend you using westacks/telebot instead of irazasyed/telegram-bot-sdk. I created it as irazasyed's was poorly documented and really buggy at a lot of places.
The two comments above helped me the most:
Use "" for your TELEGRAM_BOT_TOKEN
Instead of using your own named .env variable use TELEGRAM_BOT_TOKEN
I hope this works also for others that have this problem.

LARAVEL: main(): Failed opening required 'vendor\autoload.php'

I followed this documentation and I keep getting that main(): Failed opening required 'vendor\autoload.php' error and I ran composer install but still get the same error. I'm using Laravel and I'm calling this from a Controller..
namespace App\Http\Controllers;
require 'vendor/autoload.php';
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Google\Cloud\Speech\SpeechClient;
use Google\Cloud\Speech\StorageClient;
use App\Model\FilesModel;
use Illuminate\Support\Facades\DB;
class FilesController extends Controller
{
private $project_id;
private $speech;
private $options;
private $storage;
public function __construct()
{
$storage = new StorageClient([
'keyFile' => json_decode(file_get_contents(public_path() . '/key.json'), true)
]);
....
How do I bypass this issue?
first of all no need to do that! because it's included in all pages...
if you insist doing this I think the problem is the address of autoload file which have to be:
require '../vendor/autoload.php';
I solved deleting the "require '../vendor/autoload.php';" sentence from Controller and works in bouth environments (local and server). I am working with an Openpay integration.
That was hard for me, because i was trying to solve editing the routes or updating composer and stuff like that.

Laravel 4: Model Class not found

I am getting a Class 'App\Models\User' not found error when I try too use the User class inside a controller class method. I have looked everywhere and tried everything with no luck! Here's what I've tried:
Check that class exists and is in the right path (it works everywhere else)
Add use App\Models\User; to the top of the controller file and just use User
Tried: new \App\Models\User
Run: composer dump-autoload
Run: php artisan dump-autoload
Run: php artisan clear-compiled
When I do dd(class_exists('App\Models\User')), I get \vendor\laravel\framework\src\Illuminate\Support\helpers.php:513:boolean false which confirms that the class really isn't accessible for some reason.
Any ideas?
EDIT
You will find questions similar to this but not the same. Please read question carefully. I didn't say the controller class was missing. I said a model class (User) was not accessible from inside a particular controller class. And that the model class works everywhere else.
EDIT: Code Excerpt
<?php
use App\Models\User;
use App\Models\Role;
use App\Models\Advert;
use App\Models\AdvertPhoto;
use App\Models\AdvertMetum;
use App\Models\AdvertMetaDatum;
use App\Models\AdvertMetaCategory;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Request;
class AdvertsController extends BaseController {
/**
* Show the form for creating a new resource.
* GET /adverts/create
*
*/
public function create()
{
// New user instance
// dd(class_exists('\App\Models\User')); // Outputs FALSE
$userx = new User; // Throws an Exception
return View::make('adverts.create');
}
}
I have managed to resolve this on my own. It turns out you have to tell Laravel what class and table will be used for authentication (a.k.a your 'User' class). I didn't know this (plus this is an inherited project). Apparently the User class was defined in the root namespace (i.e. \User) and Laravel was configured to look for \User. But sometimes I see \App\Models\User in the code and it gave me the impression that User was under the same namespace as the other models since they were ALL in the app/models/ folder. I have corrected this problem in config/auth.php by changing:
'model' => 'User'
to:
'model' => App\Models\User::class
And adding namespace at the top of app/models/user.php:
namespace App\Models;
Finally I set an alias in config/app.php like this:
'User' => 'App\Models\User'
So that where ever I've been using User::blah will not break (forcing me to add use App\Models\User; everywhere!)

Yii 2 Namespace missing?

I have a namespace missing problem in Yii 2. I installed the advanced application. I am referencing a backend model from my frontend controller. Below is a code snippet of my backend model, frontend controller and error message.
Error
Unable to find 'backend\models\PaymentsMethod\TermsAndConditions' in file: C:\inetpub\wwwroot\jobmanager/backend/models/PaymentsMethod/TermsAndConditions.php. Namespace missing?
Backend Model
namespace app\models\PaymentsMethod;
use Yii;
class TermsAndConditions extends \yii\db\ActiveRecord
{
Frontend Model
public function actionCreate()
{
$model = new estimate();
$tnc = new \backend\models\PaymentsMethod\TermsAndConditions();
I have resolved my problem. I was trying to access a backend model class from a frontend controller. I resolved this by moving the backend model class to the common folder and from there I can reference it from both the backend and frontend.
Thanks
In your frontend, first include the namespace and then instantiate:
use app\models\PaymentsMethod\TermsAndConditions;
$tnc = new TermsAndConditions();
OR
As alfallouji said you can directly use:
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
If you are accessing from frontend then use frontend instead of app
i.e
namespace frontend\models\PaymentsMethod;
and if you are accessing from backend then use as below
namespace backend\models\PaymentsMethod;
You defined the model using this namespace app\models\PaymentsMethod and then you are trying to instantiate \backend\models\PaymentsMethod\TermsAndConditions.
You should be doing that in your frontend model :
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
Namespace declaration statement has to be the very first statement in the script
123456789101112
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;
class UserController extends Controller{
public function actionIndex()
{
echo "working on .....";
replace "backend" for "app" only models search
ex: app\models\PaymentsMethod;

Use elasticsearch in symfony2

I have installed elasticsearch using composer. This is my AppKernel.php file
new Elasticsearch\Client()
This is my TestController.php file.
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Elasticsearch\Client;
//use Elasticsearch\Common\AbstractFactory;
class TestController extends Controller{
/**
* #Route("/", name="test-homepage")
*/
public function indexAction(){
$client = new Elasticsearch\Client();
dump($client);
die;
}
}
I am using eclipse as my ide and it shows me error like elasticsearch\Client cannot be resolved.Why is this not working?
First of all: If you define use statement then you don't need FQCN. BTW your FQCN is not right it should starts with \ to prevent loading class from current namespace.
Then: in AppKernel.php you need to define bundles, not every library you installed.
If you have small experience in PHP try to use more easy-to-study frameworks. Symfony is mostly for experienced developers.
If you think that you can work with Symfony then I would recommend you bundle for integrating ElasticSearch and Elastica: https://github.com/FriendsOfSymfony/FOSElasticaBundle. It will save your time.
You should try to instanciate the imported class, using the short name (base on the use statements on your class), like this :
$client = new Client();
Or with the FQCN :
$client = new \Elasticsearch\Client();
(note that the new statement begin with a \ which prevent from trying to load the class from the current namespace (ie : AppBundle\Controller : \AppBundle\Controller\Elasticsearch\Client)

Categories