使用Slim PHP 2.0的Composer包

I am trying to build a REST API using Slim PHP 2.0, Composer, and a couple third-party packages. I used Composer to install Slim by creating a composer.json file in the root of my application with the following:

{
    "require": {
        "slim/slim": "2.*"
    }
}

After I ran composer install I have the following structure:

root/
    vendor/
        composer/
        slim/
        autoload.php
    composer.json
    composer.lock
    index.php

I want to include the Valitron (https://packagist.org/packages/vlucas/valitron) library to do validation along with this Bcrypt (https://packagist.org/packages/openlss/func-bcrypt) library to hash passwords for users. So, I made the following additions to my composer.json file so the it looks like this:

{
    "require": {
        "slim/slim": "2.*",
        "vlucas/valitron": "dev-master",
        "openlss/func-bcrypt": "dev-master"
    }
}

After I ran composer update I got the following directory structure.

root/
    vendor/
        composer/
        openlss/
        slim/
        vlucas/
        autoload.php
    composer.json
    composer.lock
    index.php

From here, I am not sure how to set up the autoloading for my application. I sometimes see autoload classmap and other times see psr-0. On top of these third-party packages I am going to be creating my own models to use. One will be a base model that handles connecting to the database and then each table will have a model that I use to manipulate the said table with. So for interacting with the users table I will use my UserModel.php file below. My other question is how would I go about "using" the Valitron and BCrypt files within this one? Would I just do this:

<?php namespace Libraries;

use \Valitron;
use \BCrypt;

class UserModel extends BaseModel {

    // I want to use the Valitron class here along with the crypt file

}

How would I go about setting up autoloader to accomplish this? Any help is greatly appreciated. I already dislike Composer a lot but since everyone is saying it's a must for PHP developers I am trying to force myself to learn it.

2个回答



Composer为 composer.json </ code>中指定的第三方库提供自动加载器。 请参见 https://getcomposer.org/doc/01-basic-usage.md# 自动加载。 您可以根据需要自定义自动加载器,它同时支持 PSR-4 和 类映射。 有关详细信息,请参阅自动加载参考。</ p>
</ div >

展开原文

原文

Composer provides an autoloader for third-party libraries specified in composer.json. See https://getcomposer.org/doc/01-basic-usage.md#autoloading. You can customise the autoloader for your needs, it supports both PSR-4 and classmap. See the autoload reference for more details.



我的意思是,它在现实中非常简单。 如果您希望自动加载这些类,则需要autoload.php </ p>

  require'porpors / autoload.php'; 
</ code> </ pre>
\ n

或者,在 composer.json </ code>中,您可以声明它。</ p>

  {
“autoload”:{
“psr-0 “:{{Libraries”:“vendor / open-lss”}
}
}
</ code> </ pre>

您可以这样做:</ p> \ n

  namespace Libraries \ func-bcrypt 

class bCrypt_class {

}
</ code> </ pre>

我认为你是 试图实现</ p>
</ div>

展开原文

原文

I mean, it's pretty simple in reality. If you want these classes to be autoloaded, then require autoload.php

require 'vendor/autoload.php';

Or, in composer.json you can declare it.

{
    "autoload": {
        "psr-0": {"Libraries": "vendor/open-lss"}
    }
}

Which will allow you to do:

namespace Libraries\func-bcrypt

class bCrypt_class{

}

which is what I believe you are attempting to achieve

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐