GithubHelp home page GithubHelp logo

matnex-mix / sphp Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 1.0 1.56 MB

Simplified PHP for beginers, makes projects fatser with Database management, Localizations, Admin panel, Extensions, File management, Routing and many more...

Home Page: http://getsphp.herokuapp.com

License: GNU General Public License v2.0

PHP 10.57% HTML 2.87% CSS 1.50% JavaScript 85.06%
php framework-php easy-php admin-panel

sphp's Introduction

SPHP - Simplified PHP

Simplified PHP for beginers, makes projects fatser with Database management, Localizations, Admin panel, Extensions, File management, Routing and many more.

Simple How to use

Clone the repository preferrabely into an htdocs folder where you can serve a php project

git clone https://github.com/matnex-mix/sphp.git

Edit config.json and add a database configuration follow the format there already

...

"DB": [{
  "name": "<DB_NAME>",
  "user": "<DB_USER>",
  "pass": "<DB_PASS>",
  "host": "<DB_HOST>"
}],

...

Save and view your app in the browser http://localhost/sphp

Quick Walk through

Database Migrations

Add a json file in the migrations folder let's say migrations/add_users_table.json and add the following migration commands in the json file

{
	"create": {
		"users": {
			"id": {
				"type": "int",
				"primary": true,
				"auto_increment": true
			},
			"name": {
				"type": "varchar",
				"length": 50
			},
			"created_at": {
				"type": "timestamp",
				"length": 1
			}
		}
	}
}

save then visit APP_URL/migrate, you should see a sql breakdown of the migration

Seeding a table

Edit the migrations/seeder.php file and add the below code

<?php
...

DB::seeder('users', array(
	'name' => Factory::mix('a', 5),
	'created_at' => Data::runner(function(){
		return Date('Y-m-d H:i:s');
	}),
])
	->repeat(10);

then visit APP_URL/migrate in your browser. You should see 10 different rows in your table, MAGIC right?

Use localization (different languages)

An example of this is available when you visit APP_URL/en and APP_URL/in, you should check the title bar and see the differences. Open the language file langs/english.php or langs/hindi.php and add the below code
english.php

<?php
...

L::set('hello_world', 'Hello World!');


hindi.php

<?php
...

L::set('hello_world', 'Namaste World!');

then in your pages/index.php file add

<?php
...

die( L::get('hello_world') );

now visit APP_URL/en and APP_URL/in, to check the difference

Routing

Basically, all urls correspond to a php file in /pages folder, take for example i visited APP_URL/test_page, the framework would lookup the file pages/test_page.php or pages/test_page/index.php then fallbacks to pages/404.php if it couldn't find any of the files. Create 2 new files in /pages folder

  • 404.php add <?php echo "<h2>THIS IS 404 PAGE</h2>"; ?>
  • new_page.php add <?php echo "<h2>THIS IS A NEW PAGE</h2>"; ?>

Visit APP_URL/new_page and APP_URL/eva in your browser to see the result

Adding models

Models are classes that makes quering database tables easier representing them by a PHP object for instance:

-----------------------------------
| id | name | created_at          |
-----------------------------------
| 1  | mix  | 2020-08-31 11:34:00 |
-----------------------------------

would become:

stdClass({
	->id = 1
	->name = 'mix'
	->created_at = '2020-08-31 11:34:00'
})

to use: add a new file models/users.php then add below code

<?php

class Users extends Model {

	// Set Toggle Showing Sql Query Errors
	protected $show_errors = true;

	// Set Target Table to `users`
	protected $table_name = 'users';

}

to test: edit pages/index.php and add

<?php
...

Model::load('users');

print_r( Users::get()
	->order('id', 'DESC')
	->show() );

Templating

SPHP has its own way of automatically passing data to html pages making work easier and faster. Create a new file say static/templates/test.html and add:

<h2>Title: {( @title )}</h2>
<ul>
	{( for contents )}
	<li>{( @this )}</li>
	{( endfor )}
</ul>

and add the below code in pages/index.php:

<?php
...

echo Template::parse('test', array(
	'title' => 'Testing SPHP templates',
	'contents' => [
		'Item 1',
		'Item 2',
		'Item 3'
	]
));

Database class

For database management, a substitute for MySQL queries. It has methods for the basic common sql operations:

<?php
...

# Insert into a database
var_dump(
	DB::insert('users', array(
		'name' => 'Matnex Mix',
		'created_at' => Date('Y-m-d H:i:s'),
	))
);

# Retrieve data example
# The below code uses some tables which have not been defined, note that this is just and example
var_dump(
	DB::table('history')
		->innerJoin(DB::table('users')
			->where( '>id', 0 ))
			->on([
				'history.user' => 'users.id'
			])
		->where( '*history.summary', '%' )
		->braces('||')
			->braces('&&')
				->where( 'history.id', 20 )
				->where( 'history.id', 21 )
			->close()
			->where( '+*history.time', '2020-08-02' )
		->close()
		->show()
);

# Update table
var_dump(
	DB::update('users', array(
		'name' => 'New name'
	))
		->where('id', 1)
		->run()
);

# Delete data from table
var_dump(
	DB::delete('users')
		->where('id', 1)
		->run()
);

SPhp Admin

Manage your app from within your app, sphp-admin can manage database, show errors and edit files. Visit APP_URL/admin and login with

  • username: sphp-admin
  • password: sphp_admin



Todo

  • Write real documentation
  • Add more template filters

Contribution

We're glady accepting contribution and issues, create an issue if you have one or add a pull request to contribute to this project.

Contact

[email protected]

sphp's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

maliex4mix4

sphp's Issues

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.