rduuke/newbie

PHP的微框架Web

安装: 16

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:项目

v2.2.0 2016-08-30 03:57 UTC

This package is auto-updated.

Last update: 2024-09-11 01:51:02 UTC


README

StyleCI Total Downloads Latest Stable Version Latest Unstable Version License

Newbie

安装

Composer
composer create-project rduuke/newbie
配置文件

文件 config\Config.php 从数据库中添加数据,并定义两个全局路由 BASE_URLBASE_PUBLIC

define('BASE_URL', 'http://example.com/newbie');
define('BASE_PUBLIC', 'http://example.com/newbie/public');

目录结构

/
├── bootstrap
├── config
├── public
├── resource
    ├── views
        ├── layout
├── src
    ├── Contracts
    ├── Controllers 
    ├── Models
    ├── Tools
├── vendor

HTTP层

路由

基本路由

最基本的新手路由简单接受一个URI和一个闭包,提供了一种非常简单且表达性强的定义路由的方法

    $app->get('/', function(Request $request, Response $response){
        $response->getBody()->write("Hello Word');
        return $reponse;
    });

所有新手路由都在你的路由文件 src/routes.php 中定义。可用的路由器方法:路由器允许你注册响应任何HTTP动词的路由

    $app->get($uri, $callback);
    $app->post($uri, $callback);
    $app->put($uri, $callback);
    $app->delete($uri, $callback);
    $app->patch($uri, $callback);
    $app->options($uri, $callback);
    $app->head($uri, $callback);

路由参数

必需参数

当然,有时你需要在你的路由中捕获URI的片段。例如,你可能需要从URL中捕获用户的ID。你可以通过定义路由参数来实现这一点

    $app->get('/users/{id}', function (Request $request, Response $response) {
        $id = $request->getAttribute('id');
        return $id;
    });
可选参数

偶尔你可能需要指定一个路由参数,但使该路由参数的存在是可选的。你可以通过放置 []; 确保为路由的相应变量提供一个默认值

     $app->get('/users[/{id}]', function (Request $request, Response $response) {
        $id = $request->getAttribute('id');
        return $id;
    });
路由分组

路由分组允许你在大量路由中共享路由属性,而无需在每个单独的路由上定义这些属性。共享属性以数组格式作为第一个参数指定。

    $app->group('/users', function () use ($app) {
        $this->get('', function ()); 
        //http://example.com/newbie/public/users
        $this->get('/create', function()); 
        //http://example.com/newbie/public/users/create
    });
路由控制器
    $app->group('/users', function () use ($app) {
        $controller = new RDuuke\Newbie\Controllers\UsersController($app);
        $this->get('', $controller('index'));
        $this->get('/create', $controller('create'));
    });
    ó
    // index routes (homepage, about, etc)
    $app->group('', function () use ($app) {
        $controller = new App\Controller\IndexController($app);
        $this->get('/', $controller('index'));
        $this->get('/contact', $controller('contact'));
    });

控制器

定义控制器

以下是一个基本控制器类的示例。请注意,控制器扩展了包含在 Newbie(Slim3/Controllers) 中的Controller类。Controller类提供了一些便利的函数

<?php
    namespace RDuuke\Newbie\Controllers;
    
    use MartynBiz\Slim3Controller\Controller;
    use RDuuke\Newbie\Models\Users;
    
    class UsersController extends Controller
    {
    
        /**
        * Edit method, receives numeric parameter.
        *
        * @param $id int
        */
        public function edit($id)
        {
            $user = Users::find($id);

            return view('users/edit', compact('user'));
        }
    }
资源控制器

要使用你的控制器REST结构,你必须实现ResourceController接口。方法包括

  • index
  • show($id)
  • create()
  • store()
  • edit($id)
  • update($id)
  • destroy($id)
<?php
    namespace RDuuke\Newbie\Controllers;
    
    use MartynBiz\Slim3Controller\Controller;
    use RDuuke\Newbie\Models\Users;
    
    use RDuuke\Newbie\Contracts\Controller\ResourceController;
    
    class UsersController extends Controller implements ResourceController
    {
    
        /**
        * Edit method, receives numeric parameter.
        *
        * @param $id int
        */
        public function edit($id)
        {
            $user = Users::find($id);

            return view('users/edit', compact('user'));
        }
    }

数据库

查询构建器

导入类后,我们可以使用查询构建器。

    $users = Capsule::table('users')->where('votes', '>', 100)->get();

其他核心方法可以像从DB外观一样直接从Capsule中访问

    $results = Capsule::select('select * from users where id = ?', array(1));
使用Eloquent ORM
    class User extends Illuminate\Database\Eloquent\Model {}
    $users = User::where('votes', '>', 1)->get();

有关使用此库提供的各种数据库设施的进一步文档,请参阅Laravel框架文档

视图

创建视图

视图包含由你的应用程序提供的HTML,并将控制器/应用程序逻辑与表现逻辑分开。视图存储在resources/views目录中。一个简单的视图可能看起来像这样

    <!-- view in resource/views/testing.tpl.php -->
    <html>
        <body>
            <?= $this->e($title) ?>
        </body>
    </html>

由于此视图存储在resources/views/testing.tpl.php中,我们可以使用全局视图助手返回它,如下所示

    public function Index()
    {
        $title = 'Newbie Framework';

        return view('testing', compact('title'));
    }
创建布局

为了节省在视图中重复多次相同的代码块,可以生成布局,这些布局将存储在目录resource/view/layout中。单个布局可能看起来像这样

    <!-- layout in resource/views/layout/template.tpl.php -->
    <html>
        <head>
            <title>Newbie</title>
        </head>
        <body>
            <?=$this->section('content')?>
        </body>
        </html>

在视图blog.tpl.php中使用布局

    <!-- view in resource/views/blog.tpl.php -->
    <?php $this->layout('layout/template') ?>
    <h2><?=$this->e($article->title)?></h2>
    <article>
        <?=$this->e($article->content)?>
    </article>

助手

它们位于src/Tools/Helpers.php文件中,可以在该文件中修改或添加新的助手。

redirect

接收一个字符串,包含重定向的路径。

    return redirect('users'); 
    // http://example.com/newbie/public/users
style

接收一个参数,该参数是css文件的位置 public/css/template.css,仅在视图或布局中使用。

    <head>
         <?php style('css/template.css'); ?>
         <!-- <link rel="stylesheet" href="http://example.com/newbie/public/css/template.css"> -->
    </head>
script

接收一个参数,该参数是js文件的位置 public/js/template.js,仅在视图或布局中使用。

    <body>
         <?php script('css/template.js'); ?>
         <!--  <script src="http://example.com/newbie/public/js/template.js"></script>-->
    </body>
route

获取两个必填参数和两个可选参数,必填的是URL链接名称,两个可选的是数据URL的HTML标签和属性,建议使用viees推荐或布局。示例:

    <!-- $id = 1 -->
    <?= route('users/', 'Edit', $user->id, ['class' => 'btn']) ?>
    <a href="http://example.com/newbie/public/users/1" class="btn" >Edit</a>
newFlashMessage

它需要两个必填参数和一个可选参数,用于生成闪存警报。类型:编辑 public/css/template.css

  • news #01579b
  • warning #ff6f00
  • success #004d40
  • danger #b71c1c
    public function index()
    {
        newFlashMessage('test', 'test', 'warning');
        return view('users\home');
    }
getFlashMessage

它询问是否有带有该消息的警告闪存。如果有的话返回true,否则返回false。

    <h3 class="thin">Welcome to Users!!!</h3>
    <?php if (getFlashMessage('test')): ?>
        <!-- more code -->
    <?php endif ?>
printFlashMessage

这是要显示的警报的名称,风格警报是chips materialize

    <h3 class="thin">Welcome to Users!!!</h3>
    <?php printFlashMessage('test'); ?>
    <!-- 
        <div class='chip warning'>Test
            <i class='material-icons'>close</i>
        </div> 
    -->
arrayAdd

向键值数组中添加一个项目。

    $array = ['uno' => 1 ];
    $array = arrayAdd($array, 'dos', 2);
    //['uno' => 1, 'dos' => 2];
arrayFirst

它获取数组的第一个元素。

    $array = ['uno' => 1, 'dos' => 2];
    $first = arrayFirst($array);
    //1;
arrayLast

获取数组的最后一个元素。

    $array = ['uno' => 1, 'dos' => 2]
    $last = arrayLast($array);
    //2;
arrayJson

将数组转换为JSON。

    $array = ['uno' => 1, 'dos' => 2];
    $json = arrayJson($array);
    //[{'uno':1,'dos': 2}];
jsonObject

将JSON转换为对象。

    $json = [{'uno':1,'dos': 2}];
    $object = jsonObject($json);
    //stdClass Object( [uno] => 1 [dos] => 2 ); $object->uno; 1
jsonArray

将JSON转换为数组。

    $json = [{'uno':1,'dos': 2}];
    $array = jsonObject($json);
    //array( [uno] => 1 [dos] => 2 ); $array['uno']; 1
strLimit

限制字符串中的字符数,默认为10。

    $str = 'abc def ghi';
    $strLimit = strLimit(5);
    //$strLimit = 'abc d...';
strRandom

生成一个随机字符串,根据指定的限制,默认为10。

    $strLimit = strRandom(15);
    //$strLimit = 'asbh123opkaas6x';

注意

任何建议或疑问可以发送到juuanduuke@gmail.com@RDuuke