-
-
Notifications
You must be signed in to change notification settings - Fork 533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Artisan command in Listener returns error. #10981
Comments
What do your custom routes look like? Are you looping through entries? |
Hey Duncan, that's basically correct. We get the current routes from the route facade. Then we create a new route using Laravel's Route class, then, we add those routes dynamically. public function generateDynamicRoutes(): void
{
$routes = RouteFacade::getRoutes();
$dynamicPageEntries = $this->contentService->getCollectionEntries(Collection::DYNAMIC_PAGES, ['title', 'url']);
/** @var Entry $pageEntry */
foreach ($dynamicPageEntries as $pageEntry) {
$locale = trim($pageEntry->locale());
$pageSlug = trim($pageEntry->slug());
if ($this->hasPublicRoute($pageSlug)) {
// Make sure to check originValue here in case the URL is not yet localized for non-EN entries.
if ($pageEntry->originValue('url') !== null) {
if (Str::startsWith($uri = trim($pageEntry->originValue('url')), '/')) {
$uri = Str::replaceFirst('/', '', $uri);
}
$dynamicRoute = new Route(
Request::METHOD_GET,
"/$locale/$uri",
[
'uses' => 'App\Http\Controllers\DynamicPageController@showPage',
'controller' => 'App\Http\Controllers\DynamicPageController@showPage',
'as' => "$locale.$pageSlug",
]
);
$routes->add($dynamicRoute)->middleware('web')->setContainer(app());
} else {
Log::log('debug', __METHOD__ . ' | URL skipped for' . ($pageSlug ?? '-not set-'));
}
}
}
} |
I wouldn't really recommend looping through stuff to create routes. There's probably a better way of doing what you're trying to achieve. 🤔 Why are you using a custom controller, rather than relying on Statamic's routing? |
Due to the nature of the content on some of the pages, we need extra control regarding what we can/can not show, we also want custom URLs, not based on a slug. But I don't think the routing is the root cause of the issue here. I don't think it has anything to do with it at all. Even if I did use Statamic routing, using Somehow, below code in protected function getCreatables()
{
if ($url = $this->getCreateItemUrl()) {
return [['url' => $url]];
}
$collections = $this->getConfiguredCollections();
$user = User::current();
return collect($collections)->flatMap(function ($collectionHandle) use ($collections, $user) {
$collection = Collection::findByHandle($collectionHandle);
throw_if(! $collection, new CollectionNotFoundException($collectionHandle));
if (! $user->can('create', [EntryContract::class, $collection])) {
return null;
}
$blueprints = $collection->entryBlueprints();
return $blueprints
->reject->hidden()
->map(function ($blueprint) use ($collection, $collections, $blueprints) {
return [
'title' => $this->getCreatableTitle($collection, $blueprint, count($collections), $blueprints->count()),
'url' => $collection->createEntryUrl(Site::selected()->handle()).'?blueprint='.$blueprint->handle(),
];
});
})->all();
} |
Are you able to provide a stack trace of the error you're seeing? The |
I added an error log in the issue, with a stack trace, is this sufficient? |
@duncanmcclean Snuck away some time from another project, it is re-creatable on another (basically new) install of Statamic, running on 4.58.2. This site does not use the fancy dynamic_pages setup, it's a base install of Statamic setup as multi-site. I get the same error as described in the issue. Here's another stack-trace. [2024-10-21 10:02:35] local.ERROR: Call to a member function can() on null {"exception":"[object] (Error(code: 0): Call to a member function can() on null at /home/webdev/www/base-install/vendor/statamic/cms/src/Fieldtypes/Entries.php:278)
[stacktrace]
#0 [internal function]: Statamic\\Fieldtypes\\Entries->Statamic\\Fieldtypes\\{closure}()
#1 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Collections/Arr.php(600): array_map()
#2 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Collections/Collection.php(777): Illuminate\\Support\\Arr::map()
#3 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php(404): Illuminate\\Support\\Collection->map()
#4 /home/webdev/www/base-install/vendor/statamic/cms/src/Fieldtypes/Entries.php(292): Illuminate\\Support\\Collection->flatMap()
#5 /home/webdev/www/base-install/vendor/statamic/cms/src/Fieldtypes/Relationship.php(134): Statamic\\Fieldtypes\\Entries->getCreatables()
#6 /home/webdev/www/base-install/vendor/statamic/cms/src/Fieldtypes/Entries.php(432): Statamic\\Fieldtypes\\Relationship->preload()
#7 /home/webdev/www/base-install/vendor/statamic/cms/src/Fields/Field.php(417): Statamic\\Fieldtypes\\Entries->preload()
#8 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php(60): Statamic\\Fields\\Field->meta()
#9 [internal function]: Illuminate\\Support\\HigherOrderCollectionProxy->Illuminate\\Support\\{closure}()
#10 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Collections/Arr.php(600): array_map()
#11 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Collections/Collection.php(777): Illuminate\\Support\\Arr::map()
#12 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Collections/HigherOrderCollectionProxy.php(61): Illuminate\\Support\\Collection->map()
#13 /home/webdev/www/base-install/vendor/statamic/cms/src/Fields/Fields.php(306): Illuminate\\Support\\HigherOrderCollectionProxy->__call()
#14 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Controllers/CP/Collections/EntriesController.php(484): Statamic\\Fields\\Fields->meta()
#15 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Controllers/CP/Collections/EntriesController.php(264): Statamic\\Http\\Controllers\\CP\\Collections\\EntriesController->extractFromFields()
#16 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): Statamic\\Http\\Controllers\\CP\\Collections\\EntriesController->update()
#17 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(43): Illuminate\\Routing\\Controller->callAction()
#18 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Route.php(260): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#19 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\\Routing\\Route->runController()
#20 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Router.php(806): Illuminate\\Routing\\Route->run()
#21 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#22 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/DeleteTemporaryFileUploads.php(18): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#23 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\DeleteTemporaryFileUploads->handle()
#24 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/AddVaryHeaderToResponse.php(11): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#25 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\AddVaryHeaderToResponse->handle()
#26 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/CountUsers.php(18): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#27 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\CountUsers->handle()
#28 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/BootUtilities.php(14): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#29 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\BootUtilities->handle()
#30 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/BootPreferences.php(14): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#31 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\BootPreferences->handle()
#32 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/BootPermissions.php(14): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#33 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\BootPermissions->handle()
#34 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/SelectedSite.php(15): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#35 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\SelectedSite->handle()
#36 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/Localize.php(19): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#37 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\Localize->handle()
#38 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/Authorize.php(25): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#39 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\Authorize->handle()
#40 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/AddToasts.php(22): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#41 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\AddToasts->handle()
#42 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/AuthGuard.php(14): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#43 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\AuthGuard->handle()
#44 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CP/ContactOutpost.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#45 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CP\\ContactOutpost->handle()
#46 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#47 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#48 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#49 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle()
#50 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#51 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#52 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#53 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest()
#54 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Session\\Middleware\\StartSession->handle()
#55 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#56 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle()
#57 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#58 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle()
#59 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/SwapExceptionHandler.php(19): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#60 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\SwapExceptionHandler->handle()
#61 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#62 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Router.php(807): Illuminate\\Pipeline\\Pipeline->then()
#63 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Router.php(784): Illuminate\\Routing\\Router->runRouteWithinStack()
#64 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Router.php(748): Illuminate\\Routing\\Router->runRoute()
#65 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\\Routing\\Router->dispatchToRoute()
#66 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(200): Illuminate\\Routing\\Router->dispatch()
#67 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#68 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/StopImpersonating.php(12): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#69 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\StopImpersonating->handle()
#70 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/DisableFloc.php(17): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#71 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\DisableFloc->handle()
#72 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CheckMultisite.php(14): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#73 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CheckMultisite->handle()
#74 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/CheckComposerJsonScripts.php(14): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#75 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\CheckComposerJsonScripts->handle()
#76 /home/webdev/www/base-install/vendor/statamic/cms/src/Http/Middleware/PoweredByHeader.php(18): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#77 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Statamic\\Http\\Middleware\\PoweredByHeader->handle()
#78 /home/webdev/www/base-install/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(59): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#79 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle()
#80 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#81 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#82 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#83 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#84 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#85 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#86 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#87 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#88 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(99): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#89 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#90 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#91 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\HandleCors->handle()
#92 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#93 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle()
#94 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#95 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then()
#96 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#97 /home/webdev/www/base-install/public/index.php(52): Illuminate\\Foundation\\Http\\Kernel->handle()
#98 /home/webdev/www/base-install/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php(16): require_once('...')
#99 {main}
"}
|
Thanks! We'll take a look when we can. |
Thanks, Duncan! |
Bug description
I'm running into a problem regarding caching routes using
Artisan::call
upon saving of an Entry in a specific collection.We have a collection named
dynamic_pages
, this allows our content-managers to create pages with dynamic URLs (i.e there are no route definitions inweb.php
for this).Due to the routes being dynamic, I would like to clear and then cache the routes upon saving of an Entry in this collection.
I've created created a listener that listens to the
EntrySaved
event, upon save, I check if the Entry being saved is part of thedynamic_pages
collection, if that's the case I want to clear and cache the routes again.Clearing the cache works fine, the issue arises when I try to cache the routes again. This throws a
Call to member function can() on null
which happens in theEntries.php
fieldtype because$user
is null. However, I'm calling the command from my code, so why is Statamic expecting a user?The listener:
How to reproduce
Haven't tried this on a fresh site, but seems to me it should work;
Artisan::call('route:cache')
from the listenerBlueprint:
General content fieldset
Logs
Environment
Installation
Existing Laravel app
Additional details
Multisite-setup
The text was updated successfully, but these errors were encountered: