Controller Response Types

Alloy controllers have a few useful response types that can be returned directly by the requested controller method. Note that since the response content is explicitly returned, if your controller method does not return anything, nothing will be displayed.

String Content

Returning raw strings is most useful for simple API responses that don't need their own templates. By returning a string directly, you can avoid the overhead of including and parsing another template or redundant formatting and markup code that might be unnecessary for simpler tasks.


            
// Simple strings return "Welcome " . $username; // Simple JSON API Responses return json_encode($event); // Custom user-provided XML serializer class or function return JavaAndDotNetAreCool::serializeXml($user, array('username', 'email'));

          

View Objects

There is a convenience function on the controller named 'template' that returns an instance of \Alloy\View\Template and sets the local module path and response format for you automatically. Read Alloy Views for more details on specific methods and functionality.


            
// Simple template return $this->template('indexAction'); // Template with variables passed return $this->template(__FUNCTION__) ->set(compact('posts', 'user', 'page'));

          

Resource Objects

The Resource object is a flexible response type provided primarily for web service responses. It accepts an array of data or an object that has a toArray method, and will transform the data to a JSON response (XML coming soon). As an extension of the abstract module response, it also provides methods to set an HTTP status code, errors, and more.


            
// Create and save new post $post = $kernel->mapper()->get('Module\Blog\Post'); $post->data($request->post('post')); // Gets $_POST['post'] if($kernel->mapper()->save($post)) { // Success (201 "Created" response) return $this->resource($post) ->created($post->getUrl()); } else { // Validation errors (400 "Bad Request" response) return $this->resource($post) ->status(400) ->errors($post->errors()); }

          

Any object with a __toString method

Any object that can be converted to a string (ala __toString) will be properly rendered and handled by Alloy. This allows you to create and use your own (or other) code libraries for forms, view partials, whole template engines, or whatever else you need. Since nothing is already loaded for your controller, there is no waste of unused resources or code.


            
// Example Zend_Form object $form = new Zend_Form($userFields); $form->setAction('/user/login') ->setMethod('post'); return $form;

          

Boolean False

Boolean false is a special return type that will render a generic 404 "File Not Found" HTTP response that will match the one served by Alloy for modules or actions that do not exist. In the context of a nested sub-request, a boolean false controller response will not render any content, and the primary request (and other nested sub-requests) will continue as normal, un-interrupted.


            
// Find post entity with given $id $post = $kernel->mapper()->get('Module\Blog\Post', $id); if(!$post) { return false; } return $this->template(__FUNCTION__) ->set(compact('post'));

          

Exceptions

Exceptions thrown anywhere within controller requests or nested sub-requests will stop execution and trigger an HTTP response unless caught in a try/catch block.

\Alloy\Exception\FileNotFound

Triggers a 404 "File Not Found" HTTP response with the exception message set as the response body.

\Alloy\Exception\Http

Generic and flexible way to trigger a desired HTTP response. Issues a HTTP response with the exception message set as the response body, and the exception code set as the HTTP response code.

\Alloy\Exception\Auth

Indicates the requested action requires authentication. Forces a user login and issues a 401 Unauthorized HTTP response.

Transaksi Cepat dan Mudah di Slot 5k dengan Metode Pembayaran Beragam

Dengan deposit minimal hanya 5k, transaksi di Slot 5k bisa dilakukan dengan cepat dan mudah. Banyak situs menawarkan berbagai metode pembayaran seperti e-wallet dan bank transfer, serta pembayaran instan menggunakan QRIS, yang memungkinkan pemain untuk melakukan transaksi dalam hitungan detik.

Selain menawarkan gameplay yang menarik, Mahjong Ways juga dikenal karena volatilitasnya yang seimbang, memberikan kesempatan yang baik bagi pemain untuk memenangkan hadiah besar tanpa terlalu mengandalkan keberuntungan semata. Keberadaan fitur-fitur inovatif dalam permainan ini, seperti putaran gratis yang dapat diperoleh dengan simbol tertentu, semakin meningkatkan daya tarik Mahjong Ways di kalangan para pemain slot. Dalam setiap putaran, pemain merasa tertantang untuk mencari kombinasi simbol yang tepat yang dapat menghasilkan kemenangan. Mahjong Ways terus menjadi salah satu permainan slot yang paling dicari, dengan banyak pemain yang kembali lagi untuk merasakan sensasi permainan ini.

Dalam dunia perjudian online, Slot Toto menjadi salah satu permainan yang paling diminati. Dengan kombinasi fitur menarik dan jackpot progresif, permainan ini menawarkan pengalaman bermain yang seru dan menguntungkan. Banyak pemain yang berhasil mendapatkan kemenangan besar hanya dengan modal kecil di slot toto.

Slot 777 Pengalaman Bermain Seru dan Penuh Tantangan

Slot 777 menyediakan pengalaman bermain yang seru dan penuh tantangan. Dengan berbagai pilihan permainan dan fitur menarik, platform ini memberikan peluang menang yang lebih besar bagi para pemain yang ingin meraih kemenangan besar.

Pilihan Favorit untuk Menang Besar dengan Pasaran Lengkap

Toto Togel merupakan pilihan favorit para pemain yang mencari kesempatan besar untuk menang. Dengan pasaran yang lengkap dan selalu diperbarui, situs ini memberikan peluang tinggi bagi pemain untuk memperoleh angka yang tepat dan memenangkan hadiah besar.

Keluaran Macau memberikan informasi yang sangat dibutuhkan oleh para pemain togel yang sedang mencari angka jitu. Setiap angka yang keluar di Keluaran Macau selalu menjadi topik pembicaraan hangat di kalangan penggemar togel.

Related Links