Laravel And Twitter API as well as thujohn/twitter - php

So I followed his readme and I have done composer dump-autoload a million times, but still I receive an error.
The code:
'providers' => [
...
Thujohn\Twitter\TwitterServiceProvider::class,
],
'aliases' => [
...
'Twitter' => Thujohn\Twitter\Facades\Twitter::class,
],
In my controller:
class HomeController extends Controller {
public function index() {
$tweets = Twitter::getUserTimeline([
'screen_name' => 'xxxxxxx',
'count' => 10,
'format' => 'json'
]);
dd($tweets);
return view('home');
}
public function about() {
return view('about');
}
}
But I get the error:
FatalErrorException in HomeController.php line 10:
Class 'App\Http\Controllers\Twitter' not found
Um ..... What?

You used non-namespaced name when you refered to Twitter class, so PHP is looking for the class in current namespace. Change that reference to \Twitter or add the following use statement:
use Twitter;

Related

Route is redirecting before calling the controller method

I am using Laravel 5.5.40 along with the Zizaco\Entrust Pacakge
In my routes/web.php file i have the following route setup.
Route::group(['prefix' => 'order'], function() {
Route::get('', 'OrderController#getMe');
});
It is supposed to call the getMe() method inside the OrderController.php but it instead redirects to www.mydomain.co.uk/home
namespace App\Http\Controllers;
class OrderController extends Controller
{
public function getMe() {
return "You got me!";
}
}
As a test, I added a __construct function to the OrderController.php to see if the class was even been loaded.
public function __construct() {
dd("Testing");
}
When accessing www.mydomain.co.uk/order i now get
"Testing"
I can't seem to work out why it is not running the getMe() method. Could anyone possibly shine some light on this please?
I have also tried changing the route to use ClientController#list which works fine.
Contents of ClientController.php
namespace App\Http\Controllers;
use App\Client;
class ClientController extends Controller
{
public function __construct() {
//
}
// Display all the clients
public function list() {
$tabContent = [
'display_type' => 'list',
'data' => Client::orderBy('name', 'asc')->get(),
'view_params' => [
'columns' => [
'name' => 'Client Name',
'address_line_1' => 'Address Line 1',
'town' => 'Town',
'county' => 'County',
'post_code' => 'Post Code'
],
'links' => 'client',
'controls' => True
]
];
return view('tables.list', ['data' => $tabContent]);
}
}
It has become apparent that if the controller does not have the constructor function in it, it will automatically redirect to the root of the URI with no error.
public function __construct() {
//
}

Zend Framework 3 Xampp database connection

I am new to Zend Framework 3 and I am doing this tutorial:
I have a xampp, mysql setup.
I have done everything exactly like in this tutorial. Now I am at the point where you configure the database connection. Further I have set up the controller and view.
In the tutorial link above , they are using php to create a database and then in config/autoload/global.php.....the following code:
return [
'db' => [
'driver' => 'Pdo',
'dsn' => sprintf('sqlite:%s/data/zftutorial.db', realpath(getcwd())),
],
];
I have edited this to:
'db' => [
'driver' => 'Pdo_Mysql',
'dsn' => 'mysql:dbname=dbname;host=localhost;charset=utf8;username=myuser;password=mypassword',
],
When I call the url for the index view, there the following error:
Warning: Creating default object from empty value in C:\xampp\htdocs\zendtest\module\Album\src\Controller\AlbumController.php on line 15
Fatal error: Call to a member function fetchAll() on null in
C:\xampp\htdocs\zendtest\module\Album\src\Controller\AlbumController.php
on line 22
The AlbumController:
<?php
namespace Album\Controller;
use Album\Model\AlbumTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class AlbumController extends AbstractActionController {
private $table;
public function __construct(AlbumTable $table)
{
$this->table = $table;
}
public function indexAction()
{
return new ViewModel([
'albums' => $this->table->fetchAll(),
]);
}
}
I think that the connection doesn't work??
can you share your "AlbumControllerFactory.php" ?
if you have not yet created the factory you should do.
1 - Create AlbumControllerFactory that implements FactoryInterface
2 - Inside __invoke function use the Container to inject AlbumTable to your controller
3 - config your mapping in module.config.php
'controllers' => [
'factories' => [
Controller\AlbumController::class => Controller\Factory\AlbumControllerFactory::class,
All simple, you have mistake in key $this, you did write $htis instead )

Why do you still need to use the class if it is in laravel aliases?

Why do you still need to say:
use MyClass\Table\Facades\Table;
at the top of a laravel controller
even if you have specified it in app/config/app.php
'aliases' => [
'Table' => MyClass\Table\Facades\Table::class,
You don't?
'aliases' => [
'MyClass' => Some\Vendor\Something\Facades\MyClass::class,
]
Then you can do
use Myclass;
class MyController extends Controller {
public function fetch($id) {
return MyClass::find(1)
}

Yii2 SluggableBehavior "attribute" or "value" property must be specified error

I follow Programming with YII2 tutorial on tuts+ but when I finish chapter Programming With Yii2: Sluggable Behavior and try to visit /status/ page i see next error message
Model where i connect to SluggableBehavior
namespace app\models;
use Yii;
use yii\behaviors\SluggableBehavior;
class Status extends \yii\db\ActiveRecord
{
const PERMISSIONS_PRIVATE = 10;
const PERMISSIONS_PUBLIC = 20;
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'message',
// 'slugAttribute' => 'slug',
],
];
}
. . .
What i'm doing wrong? I read definitive guide and example of usage in SluggableBehavior class in yii2 directory, but not found anything special.
After some hours of reading docs and forums about yii2 and sluggable behavior i found what i need.
I specify value property and all works fine:
'value' => function($event){
if(!empty($event->sender->slug))
return $event->sender->slug;
return Inflector::slug($event->sender->title);
},
In your case it was correct, but I hade the same issue but for other reason. I've tried to merge parent behaviour and current model behaviour and by mistake to forgot one level brackets []
public function behaviors() {
return array_merge(parent::behaviors(),
[ // <-- forgot this brackets
[
'class'=> \yii\behaviors\SluggableBehavior::className(),
'attribute'=> ['singleTranslation.title'],
'immutable' => true,
'ensureUnique' => true,
//'slugAttribute' => 'slug'
]
] // <-- forgot this brackets
);
}

Yii::configure does not work in module init()

I have a module calls API, and i want to load config file for it. The guide says that i have to use function \Yii::configure. I use it, but it doesn't apply any new configs. And i tried to use array instead config file, the result is same
class API extends \yii\base\Module
{
public $controllerNamespace = 'api\client\controllers';
public function init()
{
parent::init();
// \Yii::configure($this, require(__DIR__ . '/config/main.php'));
\yii::configure($this, [
'components' => [
'user' => [
'class' => 'yii\web\UserTest',
'identityClass' => 'api\client\models\User',
],
]
]);
echo \yii::$app->user->className();
die();
}
}
How I can override config in my module ?
UPDATE
You have to use setComponents method of Yii::$app
Yii::$app->setComponents(
[
'errorHandler'=>[
'errorAction'=>'forum/forum/error',
'class'=>'yii\web\ErrorHandler',
],
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\modules\profile\models\User',
],
]
);
OLD ANSWER
Didn't it give you errors? Your casing are wrong and so instead of "yii" in small letters use "Yii" capitalized
class API extends \yii\base\Module
{
public $controllerNamespace = 'api\client\controllers';
public function init()
{
parent::init();
\Yii::configure($this, [
'components' => [
'user' => [
'class' => 'yii\web\UserTest',
'identityClass' => 'api\client\models\User',
],
]
]);
echo \Yii::$app->user->className();
die();
}
}
I see no reason to override the application components here. I'd use #StefanoMtangoo trick but to set the component to the Module itself instead of Yii::$app:
public function init()
{
parent::init();
$this->setComponents([
'db' => [
'class' => 'yii2tech\filedb\Connection',
'path' => '#app/builder/data',
]
]);
}
Then the tricky part is to differentiate between any app's components and your module's own components. For example if my Module had a model extending yii\db\ActiveRecord I'd override its getDB() as follow (original code here):
public static function getDb()
{
return Yii::$app->getModule('api')->get('db');
// instead of: return Yii::$app->getDb();
}
So whatever the app that is using my module has or hasn't a db component it won't matter.

Categories