简体   繁体   中英

CakePHP routes of v3.1.1 don't work with v3.1.2, maybe wrong conventions?

I update my cakePHP application from v3.1.1 to v3.1.2 and in this passage all my routes and file naming broke.

with v3.1.1 :

db_table = hotels_profiles (1to1 its correctly call it in this way if i have 'hotels' table?)

ModelTable = HotelsProfilesTable

Entity = HotelsProfile

Controller = HotelsProfilesController (for call model $this->Hotelsprofiles)

url = /hotelsprofiles/action

Now with v3.1.2 i need to rename:

Controller to Hotelsprofiles, or change my url to hotels_profiles.

In controller for call model i need to rename

$this->Hotelsprofiles to $this->HotelsProfiles.

Why? What happended? I write the cake convention in wrong mode?

Please can you tell me, in which mode i need to naming files in right way if i want to add a profile to hotels table?

Upgrading isn't the cause of the problem

url = /hotelsprofiles/action

This url will, by default, look for the file:

src/Controller/HotelsprofilesController.php

The file is named correctly according to the question:

src/Controller/HotelsProfilesController.php
                     ^

That url will still work on a case-insensitive file system (windows, osx) - but will fail on a case-sensitive file system (linux).

So the cause of the problem is not upgrading, but comparing windows (version 3.1.1) to not-windows (3.1.2 - but the version isn't relevant).

Correct filenames

The conventions are detailed in the docs, though it's easier for new starters to simply use bake to ensure files and boilerplate code matches CakePHP's expectations:

-> bin/cake bake all HotelsProfiles

Welcome to CakePHP v3.1.2 Console
---------------------------------------------------------------
App : src
Path: /var/www/cakephp.dev/src/
PHP : 5.5.15-1~dotdeb.1
---------------------------------------------------------------
Bake All
---------------------------------------------------------------
One moment while associations are detected.

Baking table class for HotelsProfiles...

Creating file /var/www/cakephp.dev/src/Model/Table/HotelsProfilesTable.php
Wrote `/var/www/cakephp.dev/src/Model/Table/HotelsProfilesTable.php`

Baking entity class for HotelsProfile...

Creating file /var/www/cakephp.dev/src/Model/Entity/HotelsProfile.php
Wrote `/var/www/cakephp.dev/src/Model/Entity/HotelsProfile.php`

Baking test fixture for HotelsProfiles...

Creating file /var/www/cakephp.dev/tests/Fixture/HotelsProfilesFixture.php
Wrote `/var/www/cakephp.dev/tests/Fixture/HotelsProfilesFixture.php`
Bake is detecting possible fixtures...

Baking test case for App\Model\Table\HotelsProfilesTable ...

Creating file /var/www/cakephp.dev/tests/TestCase/Model/Table/HotelsProfilesTableTest.php
Wrote `/var/www/cakephp.dev/tests/TestCase/Model/Table/HotelsProfilesTableTest.php`

Baking controller class for HotelsProfiles...

Creating file /var/www/cakephp.dev/src/Controller/HotelsProfilesController.php
Wrote `/var/www/cakephp.dev/src/Controller/HotelsProfilesController.php`
Bake is detecting possible fixtures...

Baking test case for App\Controller\HotelsProfilesController ...

Creating file /var/www/cakephp.dev/tests/TestCase/Controller/HotelsProfilesControllerTest.php
Wrote `/var/www/cakephp.dev/tests/TestCase/Controller/HotelsProfilesControllerTest.php`

Baking `index` view file...

Creating file /var/www/cakephp.dev/src/Template/HotelsProfiles/index.ctp
Wrote `/var/www/cakephp.dev/src/Template/HotelsProfiles/index.ctp`

Baking `view` view file...

Creating file /var/www/cakephp.dev/src/Template/HotelsProfiles/view.ctp
Wrote `/var/www/cakephp.dev/src/Template/HotelsProfiles/view.ctp`

Baking `add` view file...

Creating file /var/www/cakephp.dev/src/Template/HotelsProfiles/add.ctp
Wrote `/var/www/cakephp.dev/src/Template/HotelsProfiles/add.ctp`

Baking `edit` view file...

Creating file /var/www/cakephp.dev/src/Template/HotelsProfiles/edit.ctp
Wrote `/var/www/cakephp.dev/src/Template/HotelsProfiles/edit.ctp`
Bake All complete.

In this way conventions are taken care for you, and not just the file name conventions:

...
$this->set('hotelsProfiles', $this->paginate($this->HotelsProfiles));
                                                          ^
...

You may not need the baked code - but using bake will answer many of your questions simply by looking at or using the baked code. Note the differences to the question:

  • $this->HotelsProfiles
  • Generated urls will by default be of the form /hotels-profiles/view/1 - though the exact form depends on the default route class which is configured in the routes file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM