Namespace issue in Model in Laravel - Package Development - php

I am developing a package in laravel which uses model for CRUD operations.
I have put it up in packagist as well, but when I try to install it in laravel application and visit a route defined by the package, it says
Class 'Zusamarehan\Tourify\Model\Tourifies' not found
The following is the folder structure of my package
rehan
tourify
src
assets
database
Http
Model
Tourifies.php
resources
routes
TourifyServiceProvider.php
composer.json
The following is the contents of my Tourifies.php
<?php
namespace Zusamarehan\Tourify\Model;
use Illuminate\Database\Eloquent\Model;
class Tourifies extends Model
{
}
The following is my composer.json file
{
"name": "zusamarehan/tourify",
"description": "A Package for adding Tour/Help to your Laravel Projects.",
"keywords": ["laravel", "tour", "tourify", "product-tour", "product-help"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "zusamarehan",
"email": "zrehan286#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"extra": {
"laravel": {
"providers": [
"Zusamarehan\\tourify\\TourifyServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Zusamarehan\\tourify\\": "src"
}
}
}
The Model class is not loading I suppose? I am not sure.
Can someone point out the mistake?

The namespaces in your classes use Zusamarehan\Tourify, however, in your composer.json you've used Zusamarehan\tourify. These should match.
You'll need to update your composer.json file so that the namespaces uses the correct case:
{
"name": "zusamarehan/tourify",
"description": "A Package for adding Tour/Help to your Laravel Projects.",
"keywords": ["laravel", "tour", "tourify", "product-tour", "product-help"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "zusamarehan",
"email": "zrehan286#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"extra": {
"laravel": {
"providers": [
"Zusamarehan\\Tourify\\TourifyServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Zusamarehan\\Tourify\\": "src"
}
}
}

"Zusamarehan\\tourify\\": "src" in your composer.json is wrong. Needs uppercase T. Looking at mine I also have a trailing / after src so you can try that as well.
You have the same lowercase t in the provider.

Related

How to autoload a class inside the composer file and run a script?

I'm on Drupal 9.3.x trying to add a script to run with composer, following this guide
I've tried either to add the autoload and script part to the drupal project composer.json file
{
"name": "drupal/d9-starter-kit",
"description": "Project template for D9 starter kit",
"type": "project",
"license": "GPL-2.0-or-later",
"homepage": "https://www.drupal.org/project/drupal",
"support": {
"docs": "https://www.drupal.org/docs/user_guide/en/index.html",
"chat": "https://www.drupal.org/node/314178"
},
"autoload": {
"classmap": [
"Drupal\\d9_starter_kit\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
},
// require, extra, etc...
}
or to create a composer.json inside the custom module I've made
{
"name": "drupal/d9_starter_kit",
"type": "drupal-custom-module",
"description": "a module to script the start",
"license": "proprietary",
"authors": [
{
"name": "My name",
"email": "mymail#mail.com"
}
],
"require": {},
"autoload": {
"classmap": [
"src\\Starter.php"
]
},
"scripts": {
"doStart": [
"Drupal\\d9_starter_kit\\Starter::doStart"
]
}
}
and include that to the project composer file, according the the guide on Drupal site
The path of the Start class is web/modules/custom/d9_starter_kit/src/Starter.php
<?php
namespace Drupal\d9_starter_kit;
class Starter {
public static function doStart() {
print('test');
// TODO
}
}
I've tried various other syntax for the autoload part - e.g. d9_starter_kit\\Starter.php - but none works.
I get the following error
Could not scan for classes inside "Drupal\d9_starter_kit\Starter.php" which does not appear to be a file nor a folder
Class Drupal\d9_starter_kit\Starter is not autoloadable, can not call doStart script
What is the correct way to autoload the class and run a script? The objective of the latter is the same of the guide, copy, rename and edit some files in order of automate the "start up" of a custom project template.

My bundle is not automatically registered

I created a independant bundle following the symfony tutorial but since I moved my bundle out of my app nothing work anymore.
going step by step I realized that flex doesn't auto-register my bundle into my project. Normally it should be automati as my bunlde include "type": "symfony-bundle". I think I made error in some naming (a in this issue) but I cannot put my hand on it:
Can someone with experiment can just have aquick look at the following code and explain me what I did wrong ?
//the bundle json
{
"name": "btba/chat-bundle",
"type": "symfony-bundle",
"description": "Chat generator for Symfony applications",
"keywords": ["chat", "generator"],
"homepage": "https://github.com/12rambau/Chat-bundle",
"license": "MIT",
"authors": [
{
"name": "Project Contributors",
"homepage": "https://github.com/12rambau/Chat-bundle/blob/master/CONTRIBUTORS.md"
}
],
"autoload": {
"psr-4": {
"Btba\\ChatBundle\\": "src/"
}
}
}
// the bundle load in my app
{
"type": "project",
"license": "proprietary",
"require": {
"btba/chat-bundle": "*#dev"
}
"repositories": [
{
"type": "path",
"url": "../ChatBundle"
}
]
}
// the bundle class
namespace Btba\ChatBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class BtbaChatBundle extends Bundle
{
}

PSR4 not working

I have setup a folder structure like this for a package of legacy classes
vendorname/legacy/src/ClassA.php
namespace Vendorname\Legacy;
class ClassA{}
vendorname/legacy/src/Folder/Class2.php
namespace Vendorname\Legacy\Folder;
class FolderClass2{}
With composer I'm loading this from a github repo like this:
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:username/vendorname-legacy-classes.git"
}
],
"require": {
"vendorname/legacy": "master#dev"
}
When I load ClassA like this it works:
use Vendorname\Legacy\ClassA;
$a = new ClassA();
However none of my subfolder'd classes work:
use Vendorname\Legacy\Folder\FolderClassB;
$b = new FolderClassB();
Class 'Vendorname\\Legacy\\Folder\\FolderClassB' not found
I have already defined the source folder with a file vendor\vendorname\composer.json
{
"name": "vendorname/legacy",
"description": "Vendorname Legacy classes",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"Vendorname\\Legacy\\": "src"
}
},
"extra": {
"branch-alias": {
"master": "master"
}
}
}
you need to define one thing more to your composer.json
{
"autoload": {
"psr-4": {"Vendorname\\Legacy\\": "vendorname/legacy/src/"}
}
}

Composer create local package

In my project inside the vendor directory I created a new directory named Hello and created a new class HelloWorld.php with the namespace Hello\Test. Then in the root of Hello I created a composer.json with default meta data (author, license, desc, required), and added autoload PSR-4 Hello\\Test\\.
So what do I need to do next to autoload my package. I looked at some Symfony components and their composer.json package and configuration is the same.
Is it possible to autoload my local package from vendor like this?
Dir structure:
|-my_project
|-composer.json
|---vendor
|-------composer
|-------autoload.php
|-------Hello
|-----------composer.json
|-----------HelloWorld.php
./vendor/Hello/composer.json
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": ""
}
}
}
My HelloWorld.php class has namespace Hello\Test;
Inside index.php i do include 'vendor/autoload.php
And root composer.json
{
"autoload": {
"psr-4": {
"": "src/",
"Hello\\Test\\": "./vendor/Hello"
}
},
"require": {
"Hello/Test": "*"
}
}
composer update
Okay! Sorry for the late reply.
You only need one composer.json to make this work. The one in your main project directory.
Try using this:
{
"name": "Zend/Hello",
"description": "My test package",
"license": "MIT",
"authors": [
{
"name": "Zend Zend",
"email": "test#example.com"
}
],
"autoload": {
"psr-4": {
"Hello\\Test\\": "vendor/Hello"
}
}
}
And your HelloWorld.php file should be like:
<?php
namespace Hello\Test;
class HelloWorld {
// your methods goes here
}
Run a composer self update to get the latest composer the run composer update and it should now be added to your vendor/composer/autoload_psr4.php file

How to use my Package in Laravel5

I have a question related to Package for Laravel 5. I am creating one package and tried to use that. package successfully created and using composer i can also get that in New Laravel setup , issue is that when i tried to use that it's says class not found. Here's my composer.json and Steps that i followed:
for e.g. my username = git_test and packagename = mypackage
My Package Structure :
**git_test > mypackage > src
My composer.json file
{
"name": "git_test/mypackage",
"description": "XXXXXXXXXX",
"keywords": ["laravel"],
"license": "MIT",
"authors": [
{
"name": "XXXXXXX",
"email": "XXXXXX#gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},
"minimum-stability": "dev"
}
Here's my src/myclass.php
namespace git_test\mypackage;
class myclass {
function test(){ echo "This is Test"; }
}
Now i am going to use this in my new laravel project so i add package in my directory composer and try to use the myclass in my HomeController
HomeController Code
use git_test\mypackage\myclass as TaskClass;
class HomeController extends Controller {
public function index()
{
$atTaskObj = new TaskClass('');
}
I got the error like "git_test\mypackage\myclass" Not Found. where i am doing wrong? any suggestion please.
Thanks in Advance!!!
PSR-4 paths have to end with \\:
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},

Categories