File tree Expand file tree Collapse file tree 3 files changed +10
-78
lines changed
tests/TestCase/Model/Table Expand file tree Collapse file tree 3 files changed +10
-78
lines changed Original file line number Diff line number Diff line change @@ -98,15 +98,15 @@ class ImagesController extends AppController
9898### 3. Generate URLs in Templates
9999
100100``` php
101- // Use the URL helper
102- $fileStorageTable = $this->fetchTable('FileStorage.FileStorage') ;
101+ // Generate URL to your serving controller
102+ use Cake\Routing\Router ;
103103
104104echo $this->Html->link('View File',
105- $fileStorageTable->getUrl( $fileStorage)
105+ Router::url(['controller' => 'Files', 'action' => 'serve', $fileStorage->id] )
106106);
107107
108- // This generates URL to your configured route:
109- // /images/display /{id}
108+ // This generates URL to your serving route:
109+ // /files/serve /{id}
110110```
111111
112112---
@@ -315,8 +315,11 @@ public function share($fileStorageId)
315315 'expires' => strtotime('+24 hours'),
316316 ]);
317317
318- // Generate full URL
319- $url = $fileStorageTable->getUrl($fileStorage, [
318+ // Generate full URL with signature
319+ $url = Router::url([
320+ 'controller' => 'Files',
321+ 'action' => 'serve',
322+ $fileStorage->id,
320323 '?' => $signatureData,
321324 '_full' => true,
322325 ]);
Original file line number Diff line number Diff line change 44
55use Cake \Core \Configure ;
66use Cake \ORM \Table ;
7- use Cake \Routing \Router ;
87
98/**
109 * FileStorageTable
@@ -67,47 +66,4 @@ public function initialize(array $config): void
6766 (array )Configure::read ('FileStorage.behaviorConfig ' ),
6867 );
6968 }
70-
71- /**
72- * Generate URL to file serving endpoint
73- *
74- * Applications must implement their own serving controller.
75- * This method generates the URL to your controller's action.
76- *
77- * Configure the route in config/app.php:
78- * ```
79- * 'FileStorage' => [
80- * 'serveRoute' => [
81- * 'controller' => 'Files',
82- * 'action' => 'serve',
83- * 'plugin' => false,
84- * ],
85- * ],
86- * ```
87- *
88- * @param \FileStorage\Model\Entity\FileStorage $entity File storage entity
89- * @param array<string, mixed> $options URL options (passed to Router::url)
90- *
91- * @return string URL to file
92- */
93- public function getUrl ($ entity , array $ options = []): string
94- {
95- $ route = Configure::read ('FileStorage.serveRoute ' , [
96- 'controller ' => 'Files ' ,
97- 'action ' => 'serve ' ,
98- 'plugin ' => false ,
99- ]);
100-
101- $ url = array_merge ($ route , [$ entity ->id ]);
102-
103- // Merge in query parameters
104- if (isset ($ options ['? ' ])) {
105- $ url ['? ' ] = $ options ['? ' ];
106- }
107-
108- // Check if full URL is requested
109- $ full = $ options ['_full ' ] ?? false ;
110-
111- return Router::url ($ url , $ full );
112- }
11369}
Original file line number Diff line number Diff line change @@ -86,31 +86,4 @@ public function testFileSavingArray()
8686
8787 $ this ->FileStorage ->saveOrFail ($ entity );
8888 }
89-
90- /**
91- * Test getUrl method exists and returns string
92- *
93- * Note: Full URL generation testing requires routing to be configured,
94- * which is better tested in integration tests.
95- *
96- * @return void
97- */
98- public function testGetUrlMethodExists (): void
99- {
100- $ fileStorage = $ this ->FileStorage ->newEntity ([
101- 'id ' => 'test-uuid-123 ' ,
102- 'filename ' => 'test.jpg ' ,
103- ]);
104-
105- $ this ->assertTrue (method_exists ($ this ->FileStorage , 'getUrl ' ));
106-
107- // Verify it accepts the expected parameters
108- $ reflection = new \ReflectionMethod ($ this ->FileStorage , 'getUrl ' );
109- $ this ->assertEquals (2 , $ reflection ->getNumberOfParameters ());
110-
111- $ params = $ reflection ->getParameters ();
112- $ this ->assertEquals ('entity ' , $ params [0 ]->getName ());
113- $ this ->assertEquals ('options ' , $ params [1 ]->getName ());
114- $ this ->assertTrue ($ params [1 ]->isDefaultValueAvailable ());
115- }
11689}
You can’t perform that action at this time.
0 commit comments