Giter Club home page Giter Club logo

PhpSimpleClasses

GitHub release (latest by date) Packagist Version GitHub repo size Packagist PHP Version Support GitHub

Only what is necessary to start an empty Object Oriented PHP project, with a route scheme.

Get Started:

Creating a new project PSC Based

Git Clone:

$ git clone https://github.com/PhpSimpleClasses/PhpSimpleClasses MyProject
$ cd MyProject

OR

Composer:

$ composer create-project psc/psc MyProject
$ cd MyProject

Basic Structure:

  • _core: contains all system base (classes manager, loaders, DB functions, etc).

  • public: contains the main entry point to webserver (index.php), where is you have to store all public/static files (like css, js, images, etc).

  • src: contains all backend resources, it has the MVC design pattern, but you can add or create anything you need and it will be available in any scope inside src (see more in Load Classes and Other Scripts).

  • .htaccess: (for apache) is configured to redirect any access to your domain to index.php, or if the access is to an existing file: public/myFile.png.

  • config.php: define all initial constants, like basepath and DB auth, you can add your own default constants and configs too.

  • routes.php: define all routes to public access in your project.

Basic Usage:

First, configure your context section in config.php, if you want to use a DB (MySQL), configure DB section too.

Routes:

Open routes.php, as can you see in examples routes, you must set in the $routes array, a route like key and a class/function like value. (This class have to be on Controllers).

e.g.:

$routes[] = ['/my/first', 'Test/firstFunction'];

To pass parameters, use $ as wildcard on write a route. The function on controller will receive the parameters in same order.

e.g.:

$routes[] = ['/user/$/details', 'Users/details'];

//In src/Controllers/Users.php:
//...
public function details($userId){
    echo $userId;
    //Will print any string in url between 'user/' and '/details'
}

DB:

First you need to extends the PSC class:

e.g.:

//In src/Controllers/Something.php:

namespace Controllers;

use _core\PSC;

class Something extends PSC
{
    public function __construct()
    {
        parent::__construct();
    }
}

Note: Ever write the __construct() function and lists the parent constructor to preserve the flow and prevent errors when extending the PSC class.

Then can use $this->db function:

public function getUserDetails($userId){
    $this->db->select('users', '*');
    $this->db->where('id', $userId);
    $results = $this->db->get();
    return $results;
}

All SQL methods return $this->db, then you can call all methods after first call to $this->db->...:

public function getUserDetails($userId){
    $results = $this->db->select('users', '*')
    ->where('id', $userId)
    ->get();
    return $results;
}

Anytime you can use the function $this->exec to execute SQL Querys without query builder:

public function getAllUsers(){
    $results = $this->db->exec("SELECT * FROM users");
    return $results;
}

Load Classes and Other Scripts:

Classes:

All content in src follow PHP OO default scheme. If are you running a route to Controllers/Test.php, your current namespace is Controllers, then you can call in this class:

$myOtherClass = new OtherClass();
//This will instantiate src/Controllers/OtherClass.php on $myOtherClass

But when you want to call a class in another namespace, use \ to go back to src:

$myData = new \Models\OtherData();
//$this will instantiate src/Models/OtherData.php on $myData

Other Scripts:

To load (include) other scripts without classes (like views and helpers), you can to use the function $this->load (from PSC class):

$this->load('Views/test');
//this will include src/Views/test.php

With this function you can pass all variable you need to use in script in a array, it will be extracted to use directly inside him:

$vars['name'] = 'Junior';
$vars['age'] = '21';
$this->load('Views/test', $vars);

Then inside src/Views/test.php:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello</title>
  </head>
  <body>
    Hello! My name is
    <?= $name ?>
    and i have
    <?= $age ?>
    years old.
  </body>
</html>

phpsimpleclasses's Projects

phpsimpleclasses icon phpsimpleclasses

Only what is necessary to start an empty Object Oriented PHP project, with a route scheme.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.