I have implemented API based on advanced template following Yii Rest API documentation. And I want to call API methods from backend controllers. Is it possible to do?
Thanks
So, I finally found a solution.
My Yii2 application has advanced template. I've created api module.
So app has 3 endpoints
api
backend
frontend
And I wanted to call api methods from backend or frontend, It's not important.
So the main goal here that api is the module. You can read about this here
In backend/config/main.php
'modules' => [
'api' => [
'basePath' => '#api/modules/v1',
'class' => 'api\modules\v1\Module'
]
],
And then for example
in backend/UserController/indexAction
$res = Yii::$app->runAction('api/user/index');
That how it works. Hope it will help to someone.
A way could be this .
Assuming that you rest controller is in frontend
In your backend application config you could create an additional 'UrlManager' component name eg: urlManagerForRest
return [
'components' => [
'urlManager' => [
// here is your backend URL rules
],
'urlManagerForRest' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'http://your_path/frontend/web/index.php',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
];
Then you should invoke following to compose URL:
Yii::$app->urlManagerFrontEnd->createUrl();
you can use postman for testing your api.
first download your version of postman then choose your method. then determine your inputs and your url. and finally press send button.
your url might be like
http://yourdomain.com/backend/web/index.php?r=yourController/yourAction
that yourdomain.com is your domain and yourController are controller and yourAction are your action.
Related
i have a system that has tenants with different domain. i am using stancl/tenancy for the tenancy package.
i am trying to access the /user of backpack in my tenant and i am having 302 errors on the process because backpack considers users in the tenant as guest even after login.
my route goes like this:
Route::group([
'namespace' => '\App\Http\Controllers',
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', 'universal', backpack_middleware(),
InitializeTenancyByDomain::class, ],
], function () {
Route::crud('user', 'UserCrudController');
});
Is there a way to solve this problem? Your help will be much appreciated, Thank you in Advance
I'm trying to config a RESTful API resources mapping in CakePHP3. I followed some tuts but can't make it works.
https://book.cakephp.org/3.0/en/development/routing.html#creating-restful-routes
http://www.bravo-kernel.com/2015/04/how-to-prefix-route-a-cakephp-3-rest-api/
I'm using 'prefix' route to map /api/v2/:resource with my sub-folders inside Controllers folder.
My file structure:
And this is my routing config
Router::prefix('api/v2', function ($routes) {
$routes->resources('Users');
// $routes->get('users', ['controller' => 'Users', 'action' => 'view']);
// $routes->post('users', ['controller' => 'Users', 'action' => 'create']);
// $routes->post('token', ['controller' => 'Users', 'action' => 'token']);
// $routes->fallbacks(DashedRoute::class);
});
As I read in the tuts i mention before, this should works, but i get a Missing Contoller Exception for ApiController.
If I uncomment the last line, enabling fallbacks it works fine, but it's not matching the Controller Methods with the HTTP method GET, POST, DELETE, PUT as CakePHP3 documentation mention.
Any ideas? My Cake version in 3.5.8
Thanks!!
EDIT: using bin/cake routes it's seems like the routes are fine. I'm using Postman to make request with differents HTTP methods to test this.
EDIT 2: I tried with another prefix Foo to avoid the V2 case sensitive issue, and well, this is extrage, the routes seems to be fine, but cake are not matching any of them..
Your folder name should be V2 not v2.
I believe I am missing something here. I am trying to point a route to a custom controller:
Router::scope('/myAPI/', function ($routes) {
$routes->extensions(['json', 'xml']);
$routes->resources('some_value',
[
'controller'=>'SomeValue',
'only' => ['index', 'create', 'update', 'delete'],
'id'=>'[0-9]+'
]);
});
Therefore the URL I am wanting is ~mydomain~/myAPI/some_value.
I would want this to point to the SomeValueController instead of looking for some_valueController. However the controller definition is ignored in this case and instead cakephp3 looks for some_valueController instead which I would rather avoid to try and keep with cakephp's naming conventions (and make use of the cake bake console to create a lot of controllers for models for me) but I would like to specify this custom URL.
What have I missed here?
Thanks,
Nevermind, if you TitleCase the name it works:
$routes->resources( 'SomeValue',
[
'only' => ['index', 'create', 'update', 'delete'],
'id'=>'[0-9]+'
]);
This allows the URL of some_value and points to the correct controller.
Posted just in case it helps someone else.
All data I found on the Internet is about CakePHP V2. In V3 I can't configure MongoDB with cakePHP 3. I dont know how to configure datasource for mongoDB.
My Default Databse Configuration is as follows:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'users',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
]
]
This is not a solution but will give you an insight of cakePHP 3 with mongoDB.
There is no support for mongodb datasource for cakePHP 3 as for now.The most you can do is create a new one Like ichikaway did for cakePHP 2. You can use that if you want Link Here. Fingers are crossed that someone will take an initiative and built one. You can go through the presentation of ichikaway at cakefest and get an insight for the cakePHP 2 plugin for mongodb Video Link
Edit-1: New plugin developed by lewestopher please feel free to checkout the url for further information cakephp-monga. I haven't used it yet but it's worth a try, Nice initiation.
Edit-2: Just an update that another datasource is also available by tiaguinho mongodb-cakephp3
I'm using cakePhp to create a Rest api (see http://book.cakephp.org/2.0/fr/development/rest.html) and I need to get nested resources. The documentation tells how to get let's say books implementing a URI /books.json. But does not tell how to get for example reviews for a given book. What I'm trying to make is somthing like this: /books/14/reviews.json that returns Review resources.
Can any one tell me hwo to make this?
See the Custom REST Routing section of the docs you've linked. In case the default routing doesn't work for you, you'll have to create your own custom routes that either replace or extend the default ones.
Your /books/14/reviews.json URL could for example be mapped to BooksController::reviews() likes this:
Router::connect(
'/books/:id/reviews',
array(
'[method]' => 'GET',
'controller' => 'books',
'action' => 'reviews'
),
array(
'id' => Router::ID . '|' . Router::UUID,
'pass' => array(
'id'
)
)
);
When placed before Router::mapResources() it should work fine together with the default routes.