0

I have created a extension. It is a Yii2 widget. I created a GitHub repository for it. After some issues with composer.json I have, successfully, to this Packagist package.

Now I could able to install it using composer in any yii2 appliaction using the following command:

composer require saidbakr/yii-jui-themes-list=1.2

There is a problem: it has been installed under vendor\saidbakr\yii-jui-themes-list However, I want it to be installed directly on vendor\saidbakr because of my PHP code namespace considering this namespace. So is there any way that gives me a control to where the package should be installed or at least to make my files to be placed on the repository owner directly?

arogachev
  • 33,150
  • 7
  • 114
  • 117
SaidbakR
  • 13,303
  • 20
  • 101
  • 195

2 Answers2

3

Your current namespace is defined as such

namespace vendor\saidbakr;

You should be using a something like

namespace yiiJuiThemesList;

Just because composer is going to put it into the vendor folder doesn't mean that you need your namespace as such.

cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • Well, but how to access it with `use` keyword? – SaidbakR Jan 27 '15 at 21:03
  • The same way, without `vendor`. Composer's `autoload.php` should take care of the pathing for you. – ChrisGPT was on strike Jan 27 '15 at 21:06
  • In the widget php files, I already set the namespace as regarded, but in the view `use yiiJuiThemesList\JuiThemeSelectWidget;` leads to class not found error! @Chris – SaidbakR Jan 27 '15 at 21:10
  • @sємsєм, are you using [Composer's `vendor/autoload.php`](https://getcomposer.org/doc/01-basic-usage.md#autoloading)? – ChrisGPT was on strike Jan 27 '15 at 21:11
  • @Chris I think that I does not use it because I don't know anything about it! I, just, have get a look at that file. It is only 7 lines file with only two lines of code. – SaidbakR Jan 27 '15 at 21:14
  • @sємsєм here is a good tutorial to get you started on making a package http://grossi.io/2013/creating-your-first-composer-packagist-package/ – cmorrissey Jan 27 '15 at 21:39
  • @cmorrissey Could you please take a look at the current composer.json file? https://github.com/saidbakr/yii-jui-themes-list/blob/master/composer.json – SaidbakR Jan 27 '15 at 22:44
1

Your namespaces are seriously wrong. They should not contain "vendor" in them. You should actually allow the package to install in the exact place it already does, that is exactly how it should work. Whatever you do not make them install in another place

If you want to have the namespaces simpler, take a look here: https://github.com/Mihai-P/yii2-core/blob/master/composer.json

This is how I have done it. Basically I told composer that the core namespace is actually pointing to vendor/tez/yii2-cms-module

In this way when you say

use core/widgets/blala 

it will know to go to the vendor/tez/yii2-cms-module/widgets/blala

This is how you want to set it all up.

Also you can set up an alias in yii2 too like i did here: https://github.com/Mihai-P/yii2-app-advanced/blob/master/common/config/bootstrap.php

Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • How could the namespaces that I use are wrong and they are working fine without using composer. i.e. creating a folder under vendor named `saidbakr` and then placing all files in that folder. In that case, `use vendor\saidbakr\JuiThemeSelectWidget` in the view works fine and I could able to use the widget. – SaidbakR Jan 27 '15 at 23:44
  • Show me another package that starts with the namespace "vendor" please. – Mihai P. Jan 27 '15 at 23:59
  • Should all packages start with vendor? any Yii2 packages that start with vendor? any symphony ones that are in "vendor", any packages in your project that start with "vendor". Any packages on packagist that use vendor as their main namespace? – Mihai P. Jan 28 '15 at 00:02
  • "it works" is not a good enough reason for something to be right. A square wheel works if you have enough force. – Mihai P. Jan 28 '15 at 00:04
  • Well, From your composer.json I have updated it to version 1.6 as follows: `"autoload": { "psr-4": { "saidbakr\\tools\\": "" } }` and then in the widget php files, I changed their namespaces to be `saidbakr\tools` and they works fine now. Thank you very much – SaidbakR Jan 28 '15 at 00:24
  • 1
    It looks good now, update your readme too and you are ready to rock and roll. – Mihai P. Jan 28 '15 at 00:28