{"id":20256286,"url":"https://github.com/hazardland/core.php","last_synced_at":"2025-03-03T17:54:32.235Z","repository":{"id":69835219,"uuid":"84214559","full_name":"hazardland/core.php","owner":"hazardland","description":"A framework which is a module which is the framework which is the module of the framework","archived":false,"fork":false,"pushed_at":"2017-05-28T22:23:45.000Z","size":35,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-14T03:43:17.688Z","etag":null,"topics":["framework","module","mvc","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hazardland.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-07T15:23:14.000Z","updated_at":"2017-03-22T19:04:06.000Z","dependencies_parsed_at":"2023-02-23T08:00:24.556Z","dependency_job_id":null,"html_url":"https://github.com/hazardland/core.php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazardland%2Fcore.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazardland%2Fcore.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazardland%2Fcore.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hazardland%2Fcore.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hazardland","download_url":"https://codeload.github.com/hazardland/core.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241715023,"owners_count":20007913,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","module","mvc","php"],"created_at":"2024-11-14T10:46:05.681Z","updated_at":"2025-03-03T17:54:32.221Z","avatar_url":"https://github.com/hazardland.png","language":"PHP","readme":"This is framework module. To see implementation of simple app using this framework please check https://github.com/hazardland/app.php\n\n*For detailed documentation:* https://github.com/hazardland/core.php/wiki\n\nWhen you live in PHP world while developing various web Apps you are often required to have:\n\u003c!-- MarkdownTOC --\u003e\n\n- Seo friendly nice URLs\n- With simpliest routing in background\n- Defining locales, Always having active locale\n- Generating custom urls\n- Database handling, connet to server only when connection is required\n- Abstract cache\n- Dealing with sessions\n    - Basic setup\n\n\u003c!-- /MarkdownTOC --\u003e\n\n## Seo friendly nice URLs\n\n```php\nen/about/\nfr/about/\nen/blog/123-cool-article\nen/user/123\n//see declaring routes for this links below\n```\n\n## With simpliest routing in background\n\n```php\n//this is for en/about and fr/about\n//App::getLocale() determines current locale in this case 'fr' or 'en'\nRoute::add('about','MyAboutController@defaultMethod');\n\n//for en/blog/123-cool-article\nRoute::add('blog/{post_id}-{*}', function($post_id){\n    echo $post_id;\n})-\u003ewhere('post_id',Input::INT);\n\n//for en/user/123\nRoute::add('user/{id}', 'UserProfile@index');\n\n//Named route:\nRoute::add('blog/article/{post}',function($post){})-\u003ename('article');\n//Later you an access this route by name 'article' in Route::url ...\n```\n\nWhere ```UserProfile@index``` you can imagine as file at ```./app/src/Controller/UserProfile.php```\n\n```php\n//file: ./app/src/Controller/UserProfile.php\nnamespace App\\Controller;\nclass UserProfile\n{\n    public function index ($user_id)\n    {\n        View::render('user/profile',['user_id'=\u003e$user_id]);\n    }\n}\n```\n\nOr just if you fill lazy you might do not need controller at all just parse view in route declaration:\n\n```php\n//We had this\nRoute::add('user/{id}', 'UserProfile@index');\n\n//We can this instead also:\nRoute::add('user/{id}', function($user_id)(\n    View::render('user/profile',['user_id'=\u003e$user_id]);\n));\n\n```\n\n## Defining locales, Always having active locale\n\n```php\n//every day use\nApp::getLocale()\nif (App::isLocale('en'))\n\n//locale management\nApp::addLocale('en');\nApp::addLocale('fr');\nApp::setLocale('en');;\nApp::validLocale($locale);\n\n//short version\nif (App::locale()=='en')\nif (App::locale('en'))\n\n\n//path\nApp::getPath(); #get current app route path\n```\n\n## Generating custom urls\n\nURLs with *locale* string using ```Route``` class:\n\n```php\n//If we have defined locales 'en','fr' and active locale is 'en'\n//while calling Route::url():\n\nRoute::url('blog/article/17');\n//Will return - '/en/blog/article/17'\n\nRoute::url('blog/article/17', 'fr');\n//Will return - 'fr/blog/article/17'\n\n//If your route is named as 'article'\n//Using for example Route::add('blog/article/{post}',function($post){})-\u003ename('article')\n//Then you can call Route::url passing route input parameters by array\nRoute::url('article',['post'=\u003e17]);\n//Will return - 'en/blog/article/17'\n\n//To add custom locale to URL while using route name:\nRoute::url('article',['post'=\u003e17],'fr');\n//Will return - 'fr/blog/article/17'\n\n//Of course if locale is not defined in APP\nRoute::url('blog/article/17');\n//Will return - '/blog/article/17'\n\n//Note: while passing route pass do not begin string with '/'\n```\n\nGenarating URLs using ```App``` class (without locales):\n\n```php\nApp::url('js/jquery.js');\n//Will return - '/js/jquery.js'\n\n//Note: do not start App::url string whit '/'\n```\n\nIf your App's public dir is located at server address:\n```http://myserver.com/my/app/public```\n\nThen calling ```App::url``` and ```Route::url``` will include this path:\n\n```php\nApp::url('js/jquery.js');\n//Will return '/my/app/public/js/jquery.js\n\nRoute::url('blog/article/17');\n//Will return - /my/app/public/en/blog/article/17\n```\n\nRedirecting using ```Route::redirect```:\n\n```php\n//Route::redirect() accepts same parameteres as Route::url()\nRoute::reditect ('article',['post'=\u003e17],'fr'); //Will redirect to '/fr/blog/article/17'\nRoute::reditect ('blog/article/17','fr'); //Will redirect to '/fr/blog/article/17'\nRoute::reditect ('blog/article/17'); //Will redirect to '/en/blog/article/17'\nRoute::redirect(); //Will redirect to '/en'\n\n//If yout App's public is at server's address http://myserver.com/my/app/public\nRoute::reditect ('blog/article/17');\n//Will redirect to http://myserver.com/my/app/public/en/blog/article/17\n```\n\nRedirecting using ```App::redirect```:\n\n```php\nApp::reditect('some/where'); //Will redirect to '/some/where'\nApp::reditect(); //Will redirect to '/'\n\n//If yout App's public is at server's address http://myserver.com/my/app/public\nApp::reditect('some/where'); //Will redirect to 'http://myserver.com/my/app/public/some/where'\nApp::reditect(); //Will redirect to 'http://myserver.com/my/app/public'\n```\n\n## Database handling, connet to server only when connection is required\n(And also hide connection setup from developers if needed)\n\n```php\n//config (u can have many connection definitions)\n//this line does not open connection\nDatabase::add (\n    'mysql:host=127.0.0.1;dbname=test;charset=utf8', //dsn\n    'root', //user\n    '', //password\n    [\\PDO::ATTR_ERRMODE=\u003e\\PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES=\u003efalse], //options\n    'default' //name\n);\n//usage\n//this line opens connection on first call\nDatabase::get('mySecondConenction')-\u003equery(\"SELECT 1\");\nDatabase::setDefault('mySecondConnection');\nDatabase::get()-\u003equery(\"SELECT 1\");\n```\n\n## Abstract cache\n(Some servers have APC some have APCu some of them even different caching engines) all you need is:\n\n```php\n//This line goes in config\nCache::init(new \\Core\\Cache\\Driver\\APCu());\n```\n\nCurrently core.php comes with APC and APCu cache drivers.\nTo make/use custom cache driver just implement \\Core\\Cache\\Driver interface and use it via ```Cache::init()```.\n\n```php\n//usage\nCache::set('key','value');\nCache::get('key');\nCache::exists('key');\nCache::remove('key');\nCache::clean();\n\n//Isolating app cache from another apps caches\nCache::setPrefix('myAppCachePrefix');\n```\n\n## Dealing with sessions\nThere are things you need to maintain when opening session, sometimes you need to open session with custom id, sometimes you need to have session name to separate one app session from another app session, for this you can use session class:\n\n### Basic setup\n\n*Defining custom session name.* By default php sessions does not have name. But you can have as much sessions for same domain for same client as you like. Setting session name also defines cookie name in which session id is stored in client's browser (default cookie name is PHPSESSID)\n```php\nSession::setName(\"myAppSessionName\");\n//This will not start new session\n//But if session is started will use specified name for session\n```\n\nCustom session id. In some custom scenarios you might need to set your session id before session is started in this case you cane:\n```php\nSession::setId($myCustomSessionId);\n```\n\nOpening session. While called ```Session::open()``` will use predefined session name (if any) and will use predefined session id (if any, or will use php's default session id which is provided by cookie).\n```php\nSession::open();\n\n//You can also call:\nSession::open($myCustomSessionId);\n```\n\nBasically after starting session you can use ```$_SESSION``` variable. But session class provides some additional comfort:\n\nSeparating app's session variables under same session name with custom prefix:\n```php\nSession::setPrefix('myApp');\n```\nAfter this all variables set by ```Session::set('myKey1','myValue')``` will be stored under prefix key like: ```$_SESSION['myApp']['myKey1']``` and other session variable manipulation functions will consider also app's session prefix.\n\n\nSetting and getting:\n```php\nSession::set('key','value');\nSession::get('key','default'); //if key is not set will return 'default'\nSession::all(); //returns all session variables under app's prefix\nSession::remove('key');\nSession::clean(); //removes all session variables under app's prefix\n```\n\nSession variable grouping effect:\n```php\nSession::set('aaa.key1','value1');\nSession::set('aaa.key2','value2');\nSession::set('aaa.key3','value3');\nSession::set('bbb.key1','value1');\n\n$values = Session::all('aaa.');\n//In values we have ['aaa.key1'=\u003e'value1','aaa.key2'=\u003e'value2','aaa.key3'=\u003e'value3']\n\nSession::clean('aaa.'); //Will remove 'aaa.key1','aaa.key2' and 'aaa.key3' from session\n\n//Where 'aaa.' is just a string prefix and could be anything like ending on any symbol.\n```\n\n\nClosing session\n```php\nSession::close(); //Will close session and session data will be available for next Session::open\n```\n\nDestroy session\n```php\nSession::destroy(); //will close and delete session\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazardland%2Fcore.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhazardland%2Fcore.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazardland%2Fcore.php/lists"}