2: controller-logic

Posted under » CakePHP on 11 Dec 2018

With our model created, we need a controller that handle HTTP requests and execute business logic. We’ll place this new controller in a file called ArticlesController.php inside the src/Controller directory.

<¿php
// src/Controller/ArticlesController.php
namespace App\Controller;
class ArticlesController extends AppController
{
 public function index()
  {
  $this->loadComponent('Paginator');
  $articles = $this->Paginator->paginate($this->Articles->find());
  $this->set(compact('articles'));
  }
}

By defining function index() in our ArticlesController, users can now access the logic there by requesting www.kueh.com/articles/index. Similarly, if we were to define a function called lkybast(), users would be able to access that at www.kueh.com/articles/lkybast. You may be tempted to name your controllers and actions in a way that allows you to obtain specific URLs. Resist that temptation. Instead, follow the CakePHP Conventions in creating readable, meaningful action names. You can then use Routing to connect the URLs you want to the actions you’ve created.

Our controller above fetches a paginated set of articles from the database, using the Articles Model that is automatically loaded via naming conventions. It then uses set() to pass the articles into the Template (which we’ll create next). CakePHP will automatically render the template after our controller action completes.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data