Hi,
Trying to call a route in one of my custom plugins. The plugin itself works fine but when I call a route defined in the plugin from within another plugin’s .vue file I get an error message saying route not found:
Unexpected error: No route found for path: “api/plugins/flexconfmngr/saveConfigandClose” and request method: “GET”
<?php
//error_log("Loading FlexConfMngr Plugin");
Kirby::plugin('sqw/flexconfmngr', [
'blueprints' => [
'pages/whoops' => __DIR__ . '/blueprints/whoops.yml',
'pages/yaml' => __DIR__ . '/blueprints/yaml.yml',
// ... continue adding other blueprints as needed
],
'pageMethods' => [
'getAllActiveChildren' => function () {
// Filter children where 'isactive' field in content is true
return $this->childrenAndDrafts()->filter(function ($child) {
return $child->content()->get('isactive')->toBool() === true;
});
},
'getAllInActiveChildren' => function () {
// Filter children where 'isactive' field in content is true
return $this->childrenAndDrafts()->filter(function ($child) {
return $child->content()->get('isactive')->toBool() === false;
});
},
],
'routes' => [
[
'pattern' => 'saveConfigandClose',
'method' => 'GET',
'action' => function () {
// Add your logic for saving the configuration here
error_log("saveConfigandClose function called");
// Here, you would implement the logic to save your configuration
// For example, you might gather data from active pages and save it to config.php
// After saving, you could redirect back to the main plugin page or perform another action
return [
'status' => 'success',
'message' => 'Configuration saved and closing.',
];
},
],
],
]);
// error_log("Blueprints registered for FlexConfMngr Plugin");
{“status”:“error”,“message”:“No route found for path: "plugins/flexconfmngr/saveConfigandClose" and request method: "GET"”,“code”:404,“exception”:“Exception”,“key”:null,“file”:“/kirby/src/Http/Router.php”,“line”:185,“details”:,“route”:null}
The problem was that I did not define the ‘api’=>structure to embed my ‘routes’=> structure.
Below my update code. Using this I only have to use the pattern (method name from the php) in my this.$api.get() call.
Below my vue.js code and corresponding php code in my plugin.
'api' => [
'routes' => [
[
'pattern' => 'saveConfigandClose',
'method' => 'GET',
'action' => function () {
// Add your logic for saving the configuration here
error_log("saveConfigandClose function called");
// Here, you would implement the logic to save your configuration
// For example, you might gather data from active pages and save it to config.php
// After saving, you could redirect back to the main plugin page or perform another action
return [
'status' => 'success',
'message' => 'Configuration saved and closing.',
];
},
],
],