Skip to content

therceman/copper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Copper is a PHP Framework that is mainly focused on simplicity and development speed


Versions:


Developed and Maintained by Anton (therceman) - Web Developer and Security Researcher / Bug Bounty Hunter

Twitter Bugcrowd HackerOne StackOverflow Medium Reddit


Documentation For v1.0

See The Copper Skeleton for a minimal and empty Copper v1.0 app which you can base your new apps on

Installation

composer require rceman/copper

Folder Structure

 /
  - config
    - routes.php
  - public
    - index.php
  - src
    - Controller
      - HomeController.php
  - templates
    - index.php

Configuration (Getting Started)

create file /public/index.php

<?php

require dirname(__DIR__) . '/vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new Copper\Kernel();
$kernel->handle(Request::createFromGlobals())->send();

create file /public/.htaccess

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>

Configuration (Advanced)

update file /composer.json

{
    ...
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

run console command

composer update

create file /templates/index.php

<?php /** @var \Copper\Component\Templating\ViewHandler $view */ ?>

<?= $view->render('header') ?>

<body>
<h4><?= $view->out($view->dataBag->get('message')) ?></h4>
</body>

create file /src/Controller/HomeController.php

<?php

namespace App\Controller;

use Copper\Controller\AbstractController;

class HomeController extends AbstractController
{
    public function getIndex()
    {
        $parameters = [
            'head_title' => 'App :: Home',
            'head_meta_description' => 'Application based on Copper PHP Framework',
            'head_meta_author' => '{enter your name here}',
            'message' => 'Welcome to your Application!'
        ];

        return $this->viewResponse('index', $parameters);
    }
}

create afile /config/routes.php

<?php

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

use App\Controller\HomeController;

return function (RoutingConfigurator $routes) {
    $routes->add('index', '/')
        ->controller([HomeController::class, 'getIndex'])
        ->methods(['GET']);
};

Configuration (Optional)

create file /.gitignore

vendor/
composer.lock
composer.phar

Framework modules

  • Routes config module (using config/routes.php)
  • Controller module (using Controller/*Controller.php)
  • View (Templating) module (using templates/*.php)

Routes config module :: Classes

Classes

  • [RoutingConfigurator] (Symfony) - Routing Configurator
    • Instances
      • $routes - default instance
    • Methods (basic)
      • ->add($name, $path) - Adds a route.
        • ->controller($controller) - Adds controller to a route.
        • ->methods($methods) - Sets the HTTP methods (e.g. 'POST') this route is restricted to
        • ->requirements($requirements) - Adds route requirements (e.g. ['page' => '\d+'])
        • ->defaults($defaults) - Adds route defaults (e.g. ['page' => 1])

View (Templating) module :: Variables, Methods and Classes

Variables

  • $view->request_method - Request method GET or POST
  • $view->request_uri - Request uri
  • $view->client_ip - Client's IP address
  • $view->controller_name - Controller name
  • $view->route_name - Route name

Methods

  • $view->routeBag->get($key, $default = null) - Route parameter by key
  • $view->postBag->get($key, $default = null) - POST parameter by key
  • $view->queryBag->get($key, $default = null) - GET parameter by key
  • $view->cookiesBag->get($key, $default = null) - Cookies parameter by key
  • $view->dataBag->get($key, $default = null) - Template parameter by key
  • $view->out($value) - Escape HTML code and output as string
  • $view->out($array) - Escape HTML code and output as formatted array
  • $view->render($template) - Render template
  • $view->relativePath($name, $parameters = []) - Returns the relative Path to named route
  • $view->networkPath($name, $parameters = []) - Returns the network Path to named route
  • $view->url($name, $parameters = [], $withScheme = false) - Returns the URL to named route

Classes

  • [ViewOutput] - Output processor (escape, format, etc.)
    • Instances
      • $view->output - default instance
    • Methods
      • ->raw($value) - Output as string (no escape)
      • ->js($value) - Escape Javascript code and output as string
      • ->json($array) - Format Array as JSON string (no escape)
      • ->text($value) - Escape HTML code and output as string
      • ->dump($array) - Escape HTML code and output as formatted array
  • [ParameterBag] (Symfony) - Collection of Request parameters
    • Instances
      • $view->routeBag - Route parameters (iterable)
      • $view->postBag - POST parameters (iterable)
      • $view->queryBag - GET parameters (iterable)
      • $view->cookiesBag - Cookies parameters (iterable)
      • $view->dataBag - Template parameters (iterable)
    • Methods (basic)
      • ->all() - Returns an array with parameters
      • ->get($key) - Returns a parameter by name
      • ->set($key, $value) - Sets a parameter by name
      • ->has($key) - Returns true if the parameter is defined
      • ->remove($key) - Removes a parameter
      • ->getInt($key, $default = 0) - Returns the parameter value converted to integer
      • ->getBoolean($key, $default = false) - Returns the parameter value converted to boolean

Controller module :: Methods and Classes

Methods

  • $this->viewResponse($view, $parameters = []) - Returns a Response with rendered view
  • $this->renderView($view, $parameters = []) - Returns a rendered view
  • $this->response($data, $status = 200, $headers = []) - Returns a HTTP Response
  • $this->json($data, $status = 200, $headers = []) - Returns a JsonResponse that uses json_encode
  • $this->redirectToRoute($route, $parameters = [], $status = 302) - Returns a RedirectResponse to the given route
  • $this->redirect($url, $status = 302) - Returns a RedirectResponse to the given URL

Classes

  • [Request] (Symfony) - Request represents an HTTP request
    • Instances
      • $this->request - default instance
  • [RequestContext] (Symfony) - Holds information about the current request
    • Instances
      • $this->requestContext - default instance
  • [RouteCollection] (Symfony) - A RouteCollection represents a set of Route instances
    • Instances
      • $this->routes - default instance