cakephp-3.0

Cakephp 3.5.6 disable CSRF Middleware for controller

蓝咒 提交于 2019-11-28 12:50:20
I'm trying to disable the CSRF check for a single controller (API), but I'm unable to find how I'm able to achieve this. The pre 3.5.0 CSRF Component had the ability to be disabled on certain request using: $this->eventManager()->off($this->Csrf); There are two ways to do that. Apply the middleware to specific routing scopes (or even routes) Depending on what routes you create, you can apply the middleware to specific scopes only, for example: // config/routes.php use Cake\Http\Middleware\CsrfProtectionMiddleware; Router::scope('/', function ($routes) { $routes->registerMiddleware('csrf', new

How do you modify a UNION query in CakePHP 3?

最后都变了- 提交于 2019-11-28 12:21:25
I want to paginate a union query in CakePHP 3.0.0. By using a custom finder , I have it working almost perfectly, but I can't find any way to get limit and offset to apply to the union, rather than either of the subqueries. In other words, this code: $articlesQuery = $articles->find('all'); $commentsQuery = $comments->find('all'); $unionQuery = $articlesQuery->unionAll($commentsQuery); $unionQuery->limit(7)->offset(7); // nevermind the weirdness of applying this manually produces this query: (SELECT {article stuff} ORDER BY created DESC LIMIT 7 OFFSET 7) UNION ALL (SELECT {comment stuff})

Mapping slugs from database in routing

我的梦境 提交于 2019-11-28 11:59:35
问题 I have to support url friendly structure for a project. There is multiple tables with a slug column, in cakephp how can I route the slug to a controller in the most efficient way. At first I was checking if slug exist in a table, if slug exist use the route: $c = TableRegistry::get('cateogories'); $result= $c->find()->select(['id'])->where(['url'=>$slug])->toArray(); if(count($result) > 0) { $routes->connect( '/:slug', ['controller' => 'Categories', 'action' => 'index', 'id' => $result[0]['id

How to create cookies at controller level in CakePHP 3.5?

丶灬走出姿态 提交于 2019-11-28 09:09:40
问题 I have problems to get cookies to work in cakephp 3.5.x. in earlier versions I've used the Cookie component but this is now deprecated. Its unclear for me how to use this new middlewarestuff for reading and writing cookies. The documentation is unclear for me. It shows me how to set up the cookie middleware but not how to handle creating cookies in a controller. Is there anyone who has handled cookies in 3.5.x? 回答1: The middleware only replaces the encryption part of the Cookie component

Multiple database connection in cakephp 3

早过忘川 提交于 2019-11-28 08:27:40
I am trying to connect to multiple databases in cakephp 3 . I have tried many times but all questions and answers are in cakephp 2 . I found Configuring Connections at cakephp 3 documentation. But I can't understand it. I have two databases : 1. tracking_system (Tables: trackers, events, etc..) 2. tracking_system_2 (Tables: todos, actions, etc..) Working with database tracking_system is completely running. But I don't know, how to connect to multiple databases (in my case, with second database tracking_system2 ). Thanks in advance for help. José Lorenzo Rodríguez You can declare the

How can I use my own external class in CakePHP 3.0?

南楼画角 提交于 2019-11-28 04:46:14
I am creating an application in CakePHP 3.0, in this application I want to draw SVG graphs of data using a php class that I have written. What would be the proper way to go about using this class in my CakePHP 3 project? More specifically: What are the naming conventions? Do I need to use a specific namespace? Where do I put the file that contains the PHP class? How can I include it and use it in a controller or a view? Fazal Mohammad What are the naming conventions? Do I need to use a specific namespace? Your SVG graphs class should have a namespaces. For namespaces you can see http://php.net

How to generate SQL function calls with the CakePHP query builder?

半城伤御伤魂 提交于 2019-11-28 03:55:10
问题 I have a fullname column for authors and would like to extract the surname into another column. I do that with the following raw SQL: SELECT name, SUBSTRING_INDEX(`name`, ' ', -1) AS `surname` FROM qr.authors; Output: Under "Using SQL Functions" the Cookbook says: In addition to the above functions, the func() method can be used to create any generic SQL function such as year, date_format, convert, etc. But how can I create this SUBSTRING_INDEX function through the func() method so that I can

How to load non-default models?

感情迁移 提交于 2019-11-28 03:32:34
问题 In CakePHP2.x, I have frequently used $uses attribute in controllers but it seems that this attribute is no longer available in CakePHP 3.0. The only way I know to load models which is not default one is to use loadModel() method. Is this recommended way to load models? Or is there any other way to load models? 回答1: Your observations are correct, there is no $uses property anymore, instead models/tables that do not match the controller (ex PostsTable for PostsController ) and are not

CakePHP 3 time column gets date added

邮差的信 提交于 2019-11-28 02:03:08
I have a number of columns defined in my MySQL database as "time". That is, they have a time, but not a date. When they are read by the CakePHP 3 ORM, they are being converted into Cake\I18n\Time objects (through the Cake\Database\Type\TimeType class), but the result always has both a date and a time in it, with the date set to the current date. For example, if the value is "20:00:00", debug($record['start_time']) will generate: object(Cake\I18n\Time) { 'time' => '2015-06-21T20:00:00+0000', 'timezone' => 'UTC', 'fixedNowTime' => false } And when I echo it in a template (without having used

How to use a RESTful API as ORM in CakePHP 3?

别来无恙 提交于 2019-11-28 02:00:44
问题 In CakePHP 1.x/2.x, it was fairly simple to have a model's data come from a REST API (as opposed to a relational database), by defining a custom datasource. (Neil Crookes' CakePHP-ReST-DataSource-Plugin was a great place to start.) Slap your custom datasource on your model, and you're making calls like $this->MyModel->find() just like you were querying a MySQL table called my_models . I'm trying to figure out how to achieve this same effect under CakePHP 3.0. That is, make find() / save() /