Skip to content

Instantly share code, notes, and snippets.

@jimgwhit
jimgwhit / BlogController.php
Created Jul 29, 2021 — forked from tobysteward/BlogController.php
Laravel AJAX Pagination with JQuery
View BlogController.php
<?php
class BlogController extends Controller
{
/**
* Posts
*
* @return void
*/
public function showPosts()
@jimgwhit
jimgwhit / db.php
Created May 16, 2021 — forked from luckyshot/db.php
PHP/MySQL (PDO Database) method with named parameters
View db.php
<?php
/*
PHP/MySQL (PDO) method with named parameters
---------------------------------------------
https://gist.github.com/luckyshot/9477105
Last updated: 12 Sep 17
$config = [
@jimgwhit
jimgwhit / Fetch_example.md
Last active Jun 23, 2021
Fetch simple example
View Fetch_example.md

Just test data and uptating one field using put:

    document.getElementById('submitBtn').addEventListener('click', submitPost);
    function submitPost(e) {
        e.preventDefault();
        const data = {petid: document.getElementById('petid').value,
            species: document.getElementById('species').value,
            _token: document.getElementsByName("_token")[0].value,
            _method: document.getElementsByName("_method")[0].value};
View checkbox_array.md

First, in html if a checkbox is not checked nothing is passed for that checkbox in the request.

For example if you had a form with name and likes, (pick from the following)

Name  ________________

I like:
View Array_loop_md
On a PHP coding forum someone ask how to loop a certain array.
The data was in Json format (not shown), but this will give a good idea of how to handle looping an array.
I just named the Json data $mydata.
First json decode it:
```
$decoded = json_decode($mydata, true);
// I just put the json in the variable $mydata to work with.
@jimgwhit
jimgwhit / Json_response.md
Created Jun 30, 2020
Json response with Jquery
View Json_response.md

Just quick examples, so apply validation and authentication and authorization as needed.

For returning a single record:

    public function testc() {
        $dogid = Request::input('somevar');
        $dog = Dog::find($dogid)->toArray();
        return Response::json($dog);
    }
View Upload.md
// In an update method
public function add()
{
    // Validate as needed
    $lid=DB::table('dc_dogs')->count();
    $lid=$lid+1;
    $file=Request::file('ufile');
    $file_name=$file->getClientOriginalName();
    $file_ext=$file->getClientOriginalExtension();
View Display_Image.md

At times on a shared host you may not be able to use a symbolic link. I sometimes just display from a folder outside of web folder:

A bare basic example, this file just name it DisplayImage.php

<?php
$basedir = '/some_folder/images'; // on hard drive
$imagedir = $_GET['dir'];
$image = $_GET['img'];
View Auth.md

Anyone new to RBAC I highly suggest using laravel's or Spatie https://github.com/spatie/laravel-permission

However being familiar with RBAC I use built in authentication but have custom authorization.

I use static helper classes, but instance will also work. And these are just simple examples of making sure a required role of a method matches with one of the logged in users role.

I have a role field in users table like:

   role 
View gist:5dccf7035a29b33eaa7b
entity code:
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Utility\Inflector;
/**
* Pet Entity.
*/