dswwuo1223 2014-02-22 17:59
浏览 38
已采纳

使用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条回答 默认 最新

  • dongshuobei1037 2014-02-22 18:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同