Composer.json
"autoload": {
"classmap": [
"database"
],
"files": [
"vendor/koraktor/steam-condenser/lib/steam-condenser.php"
],
"psr-4": {
"App\\": "app/"
}
},
HomeController
public function index()
{
$server = new SourceServer('80.67.11.46:27025');
try {
$server->rconAuth('abc123');
echo $server->rconExec('status');
}
catch(RCONNoAuthException $e) {
trigger_error('Could not authenticate with the game server.',
E_USER_ERROR);
}
}
I have updated the composer after adding, dump-autoload and tried all the solutions i can find with namespaces and so on.
But can't still use the steam condenser classes, any solution for this ?
The error Class 'App\Http\Controllers\SourceServer' not found denotes the fact that you're inside the App\Http\Controllers namespace and as such it will try to find the SourceServer class within that namespace. Prepend \ to your class name to call it in a global context:
$server = new \SourceServer('80.67.11.46:27025');
Or add this after the namespace declaration at the top of your controller:
use SourceServer;
And remove the class mapping from composer.json because it's not needed. You can read up more on how namespaces work in the PHP Namespaces Documentation.
Related
I am trying to create my first composer package for Codeigniter 4. However, I always get an error Class 'Myapp\Settings\Greet' not found. I'm totally lost.
I created a folder inside the ThirdParty folder named myapp-settings. Inside of that folder is another folder called src and composer.json.
Here's the content of that composer.json
{
"name": "myapp/settings",
"description": ".",
"license": "MIT",
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Myapp\\Settings\\": "src"
}
},
"require": {}
}
I created a test file inside the src folder named Greet.php
<?php namespace Myapp\Settings;
class Greet
{
public function hello()
{
return 'Hey, there!';
}
}
On codeigniter's App\Config\Autoload.php
public $psr4 = [
'Myapp\Settings' => APPPATH . 'ThirdParty/myapp-settings/src'
];
Then on codeigniter's default controller I called it.
<?php namespace App\Controllers;
use Myapp\Settings\Greet;
class Home extends BaseController
{
public function index()
{
$h = new Greet();
echo $h->hello();
}
//--------------------------------------------------------------------
}
Once I run it I got an error Class 'Myapp\Settings\Greet' not found. APPPATH\Controllers\Home.php at line 9. How can I fix this?
Instead of editing the codeigniter's app/Config/Autoload.php, revert it and add these lines to the project composer.json:
"repositories": [
{
"type": "path",
"url": "app/ThirdParty/myapp-settings"
}
],
// "require": {
Then run composer require myapp/settings
Since I have also stuck at this problem, a GitHub repository is created just for it. You might check and clone, watch the commits to understand the steps.
Following this blog post I successfully implemented Class-based factories in Laravel.
Initially I put my ModelFactorys in app/some/folder. Everything was totally fine. But I want them in database/factories/classbased, and then this error started;
PHP Fatal error: Cannot declare class Factories\ClassBased\GroupFactory, because the name is already in use in ..root/project/database/factories/classbased/GroupFactory.php on line 18
I have had a good look on SOF and elsewhere about this error, but all the answers seem to be about different reasons/ I can't transpose the answers given to my situation.
I have tried renaming my classes to something totally unique (as ModelFactory is already a naming format used by Laravel for its standard factories) but I still got the same error. My custom classes seem to be re-declaring and I don't know why.
This is what I have for my custom class:
<?php
// database/factories/classbased/GroupFactory.php
namespace Factories\ClassBased;
class GroupFactory
{
// stuff //
public function facilitatedBy(TeamMember $teamMember)
{
$this->facilitator = $teamMember;
return $this;
}
This is what I have in my test:
<?php
namespace Tests\Feature;
use XYZ;
use Facades\Factories\ClassBased\GroupFactory;
class ClassBasedGroupFactoryTest extends TestCase
{
use RefreshDatabase, SetUpRolesAndPermissions, WithFaker;
public function setUp() : void
{
parent::setUp();
$this->setUpRolesAndPermissions();
}
/** #test */
public function it_can_set_the_facilitator()
{
$facilitator = $this->createTeamMemberWithRoleOf('facilitator');
$group = GroupFactory::facilitatedBy($facilitator)->create();
$this->assertEquals($facilitator->id, $group->facilitator->id);
}
/** #test */
public function another_test()
{
$x = 'y';
$group = GroupFactory::someThing($x)->create();
$this->assertEquals($x, $group->theThing);
}
And I have this in my composer.json:
"autoload": {
"files": [
// files
],
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories",
"database/factories/classbased"
]
},
I would expect to be able to use the class without issue, as I did when it was in the App namespace. But when I refactored to this then the errors started.
What do I need to do to fix this?
I think that the issue is not existing anymore, but I'm adding a possible solution for those who might stumble across this thread.
Use a psr-4 autoloader inside composer.json and put the factories on same level and not in a subdirectory of factories.
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Classbased\\": "database/classbased/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
Update your files to reflect the proper namespace and directory structure.
<?php // database/classbased/GroupFactory.php
namespace Factories\ClassBased;
class GroupFactory {
Sidenote: I'm still puzzled why using a subdirectory inside factories/ was not working for me.
I just figured out how to install and use PHP composer and used it to instal php-sql-query-builder to my project. The system created the vendor folder, etc. however I am having issues using classes within the package. It gives me the following error, any suggestions on how I can fix this?
Fatal error: Uncaught Error: Class 'NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder' not found in D:\Documents\CadetPortal\php\lib\login.class.php on line 15
Login.class.php
require_once ("core.class.php");
require_once ("../../vendor/autoload.php");
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
class LoginSystem {
private $core;
private $builder;
private $config;
function __construct(){
$this->core = new coreFunctions();
$this->builder = new GenericBuilder();
$this->config = require('core.config.php');
}
//....
}
EDIT
fncregister.php
require_once "../../vendor/autoload.php";
$LoginManager = new \ThomasSmyth\LoginSystem();
echo $LoginManager->Register($_POST["StrSurname"], $_POST["StrForename"], $_POST["StrEmail"], $_POST["StrPassword"], $_POST["DteDoB"], $_POST["StrGender"], $_POST["StrToken"]);
composer.json
{
"require": {
"nilportugues/sql-query-builder": "^1.5"
},
"autoload": {
"psr-4": {
"ThomasSmyth\\": "php/lib/"
}
}
}
Your class source files shouldn't have any require_once statements at all in them. Follow the PSR-4 spec for naming. Put your classes in a namespace to avoid collision with other classes you might include via composer. Then put one class in one file, named the same as the class. For example, the LoginSystem class should be in a file named LoginSystem.php.
namespace MyNamespace;
class LoginSystem
{
...
}
Then set your composer.json to point your namespace to your source directory:
"autoload": {
"psr-4": {
"MyNamespace\\": "src/"
}
},
Now, your main app invoker or front controller should be the only place that includes the autoloader:
require_once 'vendor/autoload.php';
$login = new \MyNamespace\LoginSystem();
...
I am new to Laravel. I want use some Own Functions. Where do Write the Function.
<?php function userrole1($roleid) {
$userrole=DB::table('roles')->where('id', '=', $roleid)->get();
?>
#foreach($userrole as $val)
<?php echo $val->role_title; ?>
#endforeach
<?php
}
?>
New Way to add Helpers
1: I created folder app/Helpers
2: In app/Providers I created new provider file HelperServiceProvider.php
3: In this file I registered all helpers classes I need
$this->app->bind('dateHelper', function()
{
return new \App\Helpers\DateHelper;
});
In config/app.php I added this new provider
'App\Providers\HelperServiceProvider',
Use This helper function dateHelper
Old Way
Create a helpers.php file in your app folder and load it up with composer:
"autoload": {
"classmap": [
...
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php" // <---- ADD THIS
]
},
After adding this run composer dump-autoload command in cmd
You need to create and register your own helpers file:
http://laravel-recipes.com/recipes/50/creating-a-helpers-file
After that you'll be able to use custom helpers (functions) in your app.
Just make a function in the model class and include model and call it from the controller as pass from there to the view using variable. thats it.
In Model User(you can make any):
public function userrole1($roleid) {
$userrole=DB::table('roles')->where('id', '=', $roleid)->get();
return $userrole
}
In Controller:
use App\User
public function __construct(User $user){
$this->user_model = $user;
}
public function index(){
$userRole = $this->user_model->userrole1()
return view('admin/index', ['userRole' => $userRole]);
}
I want to use Doctrine 1 inside a Zend Framework 2 project. And it has the old underscore/directory class naming style. If I am right that is compatible with the PSR0 autoloading. So I configured it as I thought would be correct. But it's not. :-(
I get the following error, when accessing my AlbumController via browser:
Fatal error: Class 'AlbumApi\Controller\Doctrine_Query' not found in /project/application_zf2/module/AlbumApi/src/AlbumApi/Controller/AlbumController.php on line [...]
Where is my misconception?
This is my project structure
/project
/application
/application_zf2
/module/AlbumApi/src/AlbumApi/Controller
/AlbumController.php
/composer.json
/init_autoloader.php
/library
/Doctrine
/Doctrine/MoreDirectories
/Doctrine.php
composer.json:
{
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": ">=2.2.4",
"zendframework/zend-developer-tools": "dev-master"
},
"include-path": ["../library/Doctrine"],
"autoload": {
"psr-0": {
"Doctrine_": "../library/Doctrine"
}
}
}
AlbumController
<?php
namespace AlbumApi\Controller;
use AlbumApi\Controller\AbstractRestfulJsonController;
use Zend\View\Model\JsonModel;
class AlbumController extends AbstractRestfulJsonController
{
public function getList()
{ // Action used for GET requests without resource Id
$query = Doctrine_Query::create()
->from('User b')
->where('b.plz LIKE ?', $plz.'%');
$result = $query->fetchArray();
return new JsonModel($result);
}
}
Doctrine 1 doesn't use namespaces, so you have to write \Doctrine_Query instead of just Doctrine_Query.