Skip to content

Commit

Permalink
example directory added. readme file updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertugfahriozer committed Nov 27, 2022
1 parent 7a529ca commit 3f348a4
Show file tree
Hide file tree
Showing 12 changed files with 646 additions and 8 deletions.
19 changes: 19 additions & 0 deletions example/Common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* The goal of this file is to allow developers a location
* where they can overwrite core procedural functions and
* replace them with their own. This file is loaded during
* the bootstrap process and is called during the frameworks
* execution.
*
* This can be looked at as a `master helper` file that is
* loaded early on, and may also contain additional functions
* that you'd like to use throughout your entire application
*
* @link: https://codeigniter4.github.io/CodeIgniter4/
*/

function show_404(){
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
76 changes: 76 additions & 0 deletions example/app/Config/Filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Filters\CSRF;
use CodeIgniter\Filters\DebugToolbar;
use CodeIgniter\Filters\Honeypot;
use CodeIgniter\Filters\InvalidChars;
use CodeIgniter\Filters\SecureHeaders;

class Filters extends BaseConfig
{
/**
* Configures aliases for Filter classes to
* make reading things nicer and simpler.
*
* @var array
*/
public $aliases = [
'csrf' => CSRF::class,
'toolbar' => DebugToolbar::class,
'honeypot' => Honeypot::class,
'invalidchars' => InvalidChars::class,
'secureheaders' => SecureHeaders::class,
];

/**
* List of filter aliases that are always
* applied before and after every request.
*
* @var array
*/
public $globals = [
'before' => [
'honeypot',
//if csrf is active you must make whitelist.
'csrf' => ['except' => ['addBasket',
'updateBasket',
'removeBasketOne',
'emptyTheBasket']],
// 'invalidchars',
],
'after' => [
'toolbar',
'honeypot',
// 'secureheaders',
],
];

/**
* List of filter aliases that works on a
* particular HTTP method (GET, POST, etc.).
*
* Example:
* 'post' => ['foo', 'bar']
*
* If you use this, you should disable auto-routing because auto-routing
* permits any HTTP method to access a controller. Accessing the controller
* with a method you don’t expect could bypass the filter.
*
* @var array
*/
public $methods = [];

/**
* List of filter aliases that should run on any
* before or after URI patterns.
*
* Example:
* 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']]
*
* @var array
*/
public $filters = [];
}
55 changes: 55 additions & 0 deletions example/app/Config/Routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
require SYSTEMPATH . 'Config/Routes.php';
}

/**
* --------------------------------------------------------------------
* Router Setup
* --------------------------------------------------------------------
*/
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();

/**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->add('/', 'Home::index');
$routes->get('productList', 'Home::productList');
$routes->get('basket','Product::basket');
$routes->post('successfullBasket','Product::successfullBasket',['as'=>'successfullBasket']);
$routes->post('addBasket','Product::addBasket');
$routes->post('updateBasket','Product::updateBasket');
$routes->post('removeBasketOne','Product::removeBasketOne');
$routes->post('emptyTheBasket','Product::emptyTheBasket');

/**
* --------------------------------------------------------------------
* Additional Routing
* --------------------------------------------------------------------
*
* There will often be times that you need additional routing and you
* need it to be able to override any defaults in this file. Environment
* based routes is one such time. require() additional route files here
* to make that happen.
*
* You will have access to the $routes object within that file without
* needing to reload it.
*/
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}
57 changes: 57 additions & 0 deletions example/app/Controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace App\Controllers;
/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
* class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*
* @package CodeIgniter
*/

use ci4shoppingcart\Libraries\Cart;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

abstract class BaseController extends Controller
{
/**
* Instance of the main Request object.
*
* @var CLIRequest|IncomingRequest
*/
protected $request;

/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = [];
public $defData;
/**
* Constructor.
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);

//--------------------------------------------------------------------
// Preload any models, libraries, etc, here.
//--------------------------------------------------------------------
// E.g.:
// $this->session = \Config\Services::session();
$this->defData=['cart'=>new Cart()];
}
}
31 changes: 31 additions & 0 deletions example/app/Controllers/Home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;

class Home extends BaseController
{
use ResponseTrait;

public function productList()
{
$this->defData['products'] =
[
['id' => 1,
'productTitle' => 'NortPas Şınav Tahtası',
'productCategory' => 'Spor Malzemeleri',
'price'=>24,
'stock' => 34],
['id' => 2,
'productTitle' => 'HP Pavilion G135',
'productCategory' => 'Laptop',
'price'=>47,
'stock' => 3],
['id' => 3,
'productTitle' => 'Logitech Sessiz Fare',
'productCategory' => 'Mouse',
'price'=>8,
'stock' => 90]
];
return view('products/productList', $this->defData);
}
}
95 changes: 95 additions & 0 deletions example/app/Controllers/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\CommonModel;
use CodeIgniter\API\ResponseTrait;

class Product extends BaseController
{
use ResponseTrait;

public function basket()
{
return view('products/basket', $this->defData);
}

public function addBasket()
{
if ($this->request->isAJAX()) {
$basket = ['id' => $this->request->getPost('id'),
'qty' => $this->request->getPost('qty'),
'price' => $this->request->getPost('price'),
'name' => $this->request->getPost('title')];
if ($this->defData['cart']->insert($basket))
return $this->showCart();
else
return 'ürün sepete eklenemedi.';
} else return show_404();
}

public function updateBasket()
{
if ($this->request->isAJAX()) {
$basket = ['rowid' => $this->request->getPost('rowid'),
'qty' => $this->request->getPost('qty')];
if ($this->defData['cart']->update($basket))
return $this->showCart();
} else return show_404();
}

public function removeBasketOne()
{
if ($this->request->isAJAX()) {
if ($this->defData['cart']->remove($this->request->getPost('rowid')))
return $this->showCart();
} else return show_404();
}

public function emptyTheBasket()
{
if ($this->request->isAJAX()) {
return $this->defData['cart']->destroy();
} else return show_404();
}

private function showCart()
{
$carts = $this->defData['cart']->contents();
$array = [];
foreach ($carts as $cart) {
$array['carts'][$cart['rowid']] = [
'id' => $cart['id'],
'qty' => $cart['qty'],
'price' => $this->defData['cart']->format_number($cart['price']),
'name' => $cart['name'],
'subtotal' => $this->defData['cart']->format_number($cart['subtotal']),
'rowid' => $cart['rowid']
];
}
$array['count'] = count($array['carts']);

return $this->respondCreated($array);
}


public function successfullBasket()
{
$commonModel = new CommonModel();
$pnr=$this->generateActivateHash(4);
foreach ($this->defData['cart']->contents() as $item) {
$data = ['productPnr' => $pnr,
'productTitle'=>$item['name'],
'price'=>$item['price'],
'qty'=>$item['qty'],
'subtotal'=>$item['subtotal']];
$commonModel->create('products', $data);
}
$this->defData['cart']->destroy();
return view('products/success', $this->defData);
}

public function generateActivateHash($bit = 16)
{
return bin2hex(random_bytes($bit));
}
}
Loading

0 comments on commit 3f348a4

Please sign in to comment.