PHP code example of jhuddle / framework

1. Go to this page and download the library: Download jhuddle/framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jhuddle / framework example snippets


  <ul>
     foreach($items as $item): 

  

  // Adjust paths to match your project structure:

  

  

  // Instantiate app and define it as a constant:

  define('app', new \Framework\App([
    'view' => dir('../templates'),
  ]));

  // Declare routes:

  app->route->get('/',
    function () {
      print app->view('layout', [
        'title' => "Home page",
        'content' => "This is the home page.",
      ]);
    }
  );

  app->route->get('/hello/<name



define('app', new \Framework\App([
  'layout' => dir('../templates/layouts'),
  'partial' => dir('../templates/partials'),
]));

print app->layout('song/lyrics', [
  'title' => "The Lonely Goatherd",
  'content' => implode("<br>", [
    "High on a hill was a lonely goatherd,",
    app->partial('yodel.php'),
    "Loud was the voice of the lonely goatherd,",
    app->partial('yodel.php', ['short' => true]),
    "Folks in a town that was quite remote heard",
    app->partial('yodel'),  // adds `.php` if no suffix given
    "Lusty and clear from the goatherd's throat, heard",
    app->partial('yodel', ['short' => true]),
  ]),
]);



define('app', new \Framework\App([
  'notes' => ["Do", "Re", "Mi",],
  'lineFrom' => fn ($a, $b) => "$a - $b,\n",
]));

print "When you read you begin with A-B-C.\n";
print "When you sing you begin with " . implode("-", app->notes) . ".\n";

app->use([
  'notes' => ["Do", "Re", "Mi", "Fa", "So", "La", "Ti",],
  'memory_aids' => [
    "a deer, a female deer",
    "a drop of golden sun",
    "a name I call myself",
    "a long, long way to run",
    "a needle pulling thread",
    "a note to follow So",
    "a drink with jam and bread",
  ],
]);

foreach (array_combine(app->notes, app->memory_aids) as $note => $memory_aid) {
  print app->lineFrom($note, $memory_aid);
}
print "That will bring us back to " . app->notes[0] . "!";



define('app', new \Framework\App());

app->route->get('/html',
  fn () => print app->request->get(
    'https://github.com/jhuddle/framework/blob/main/README.md'
  )
);

app->route->get('/json/<data:string>',
  function ($data) {
    $round_trip = app->request->post(
      'https://httpbin.org/anything',
      [
        'Content-Type' => 'text/plain',
        'X-Powered-By' => 'jhuddle/framework',
      ],
      $data
    );
    print "<h1>" . $round_trip->headers['Content-Type'] . "</h1>";
    print "<pre>" . $round_trip . "</pre>";
  }
);



// Using default implementation with environment variables
// `DBMS=mariadb` + `DB_` credentials (see above):

define('app', new \Framework\App());

$something_good = app->db->execute(
  '
    SELECT thing
    FROM youth INNER JOIN childhood ON youth.person_id = childhood.person_id
    WHERE thing LIKE ?
    LIMIT 1
  ',
  ['%good%']
);

// Switching to SQLite:

app->use([
  'db' => new \PDO('sqlite:nothing.sqlite')
]));

$query = app->db->query('
  SELECT thing
  FROM youth INNER JOIN childhood ON youth.person_id = childhood.person_id
  WHERE thing IS NULL
');

$nothing = $query->fetchAll(\PDO::FETCH_OBJ);

  

  define('app', new \Framework\App([
    'view' => dir('../templates'),
  ]));

  $confidence = app->view('mantra', [
    'attribute' => 'confidence',
    'item' => 'me',
  ]);

  app->route->get('/confidence/<item>',
    fn ($item) => print $confidence->use(['item' => $item])
  );
  

  

  define('app', new \Framework\App([
    'view' => dir('../templates'),
  ]));

  app->route->get('/sound/<noise

  

  define('app', new \Framework\App([
    'view' => dir('../templates'),
  ]));

  app->route->get('/edelweiss',
    app->view('layout', [
      'title' => "Edelweiss",
      'content' => "<p>Every morning you greet me!<p>",
    ])
  );
  

  

  class RelationshipController {

    public static function depend() {
      print "I'll depend on you";
    }

    public static function takeCare() {
      print "I'll take care of you";
    }

    public static function prepare(string $men) {
      $sanitized_men = htmlspecialchars(urldecode($men));
      print "$sanitized_men - what do I know of those?";
    }

  }

  define('app', new \Framework\App());

  app->route->prefix('sixteen')
    ->get('/seventeen', [RelationshipController::class, 'depend'])
    ->get('/eighteen', [RelationshipController::class, 'takeCare'])
    ->get('/<men:string>', [RelationshipController::class, 'prepare'])
  ;