简体   繁体   中英

How can I composer package on the server that I have installed on my local machine?

Via Terminal I installed the Ramsey/Uuid plugin:

composer require ramsey/uuid

In my PagesRepository.php I am creating this function:

use Ramsey\Uuid\Uuid;

  function generateUid()
  {
     $uuid = Uuid::uuid4();
     $uuid = $uuid->getHex();
     $uuid =  substr($uuid,0,10);

     return   $uuid;
  }

I am using the function in my PagesController.php

  $unique_id = PagesRepository::generateUid();
  $entity->setUnique_id($unique_id);

It is working very fine on my local machine, but then I push it via git to my server and there I get the error message:

Attempted to load class "Uuid" from namespace "Ramsey\\Uuid". Did you forget a "use" statement for "Symfony\\Component\\Validator\\Constraints\\Uuid"?

I cannot install anything on the server because it is a shared host and the permission is denied so I copied the ramsey folder into my vendor folder on the server. But this was not solving the problem.

I do not understand, why this is asking for the Symfony validator, as it does not need it on the local machine.

Its because of composer auto generated files not linking your folder or class ( ramsey ) .

So, as i understand you can not run composer update or composer install on shared hosting, you should upload whole updated vendor folder to your server.

Then auto generated files like vendor\\composer\\autoload_psr4.php will be updated and have link to class like Ramsey\\Uuid\\Uuid .

Point is

PHP Namespace can not include class/file automatically. It loads classes with composer's auto generated linking files and __auload magic function.

If you are not running composer install in your server, you need to copy the whole vendor folder, at least, to wherever you are running the system.

The package you have installed might have installed other dependencies, for example, but you are not copying them when transferring only the files for that package. And at the very least you'll need the generated autoloader files.

Since you creating the installation on dev and transferring it to production, I would advise delete your local vendor folder and re-do the whole installation first:

composer install --prefer-dist --no-dev --no-scripts --no-progress --no-suggest --classmap-authoritative --no-interaction

And transfer that vendor folder to production. You should make sure that the installation process doesn't include any other step (enabling bundles, creating configuration files, etc), but I'm hoping you took care of that at some point.

After yo do that, you can reset the previous steps, and do a regular composer install for development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM