REST Service not working-Code Igniter - php

I want to implement a Phill Sturgeon CodeIgniter RESTServer library in my project. I copied the files rest.php, Format.php, REST_Controler.php in folders config,library,library respectively.
I created my controller called services with following code:
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class services extends REST_Controller {
function Teams_get(){
$teamNames=$this->team_model->getTeamNames();
$this->response($teamNames);
}
TeamModel is autoloaded in my autoload.php. When I want to run Teams_get method in my browser result is:
{"status":false,"error":"Unknown method."}
I read here that I should change REST_Controler.php configuration file, but this change should only be done if POST methods are not working.
My services should be public, so I don't need authentication methods.
What's wrong here?

When calling your API, the URL should just be the name of the method, without the _get (or _post). That is added by the REST server depending on how the URL is called (GET vs POST).
So, to call your Teams_get method, you want to send a GET request to the URL /services/Teams (not /services/Teams_get).
Docs: https://github.com/philsturgeon/codeigniter-restserver#handling-requests

Related

Getting an error when trying to add component to AppController in CakePHP

Im trying to add a SessionComponent to my controller in order to able to change the language of my app on the fly. The following snippet (specifically line 3) is the code i've tried according to http://book.cakephp.org/2.0/en/controllers/components.html#using-components
class AppSettingsController extends AppController
{
var $components = array('Session');
But when i try to run any of the actions of my Controller i get:
SessionComponent could not be found.
Create the class SessionComponent below in file:
src/Controller/Component/SessionComponent.php
As if the SessionComponent doesn't exist. All answers i have been able to find say that what i have already done ought to work. Do you have any idea what i might have missed or what i should look into to fix it?
From your post I conclude that you are referred to a CakePHP 2 documentation, but error message telling us that you are using CakePHP 3!
src/Controller/Component/SessionComponent.php
cakephp 3 has the following components:
Authentication
Cookie
Cross Site Request Forgery
Flash
Security
Pagination
Request Handling
and here is how to use sessions in your application:
http://book.cakephp.org/3.0/en/development/sessions.html#accessing-the-session-object

Calling a function in Magento Admin Module Controller via URL

I was under the impression (after viewing some tutorials on Alan Storms site about Models) that I should be able to call a function on my controller via a url like so:
http://www.localhost.com:8080/magento/index.php/mymodule/adminhtml_mymodule/someFunction
and in the controller declare:
public function someFunctionAction()
{
Mage::log("Im In");
}
The problem is that nothing is being logged. Is there something special with admin modules that prevents this from working?
Note: I haven't included the rest of my code for declaring the module as everything is working fine, I am merely curious about calling the controller function via the Url in this way but please let me know if you require more information in order to answer it properly.
You can't call your action with the direct URL, because Magento uses nonces in the admin section. You can read about this here: http://alanstorm.com/magento_admin_hello_world_revisited , look for the "Magento Admin URLs"-section.
Try this, if your controller name is poductController than in URL use product
http://www.localhost.com:8080/magento/index.php/mymodule/product/someFunction

Easy way to find the controller file with only the URL on Cake PHP

Being new to Cake on PHP, I am trying to work out if I have a URL, what would be the easiest way to find the controller code for it?
The URL on my local machine is something like:
http://foofoofoo.local/protected/admin/org/edit/1
I have worked out that the location of the view for this file is at this location on my machine:
/var/www/MyApp/protected/app/views/org/admin_edit.ctp
I thought what I'd do is do a search throughout the entire codebase for anything referencing admin_edit.ctp. I found two entries, and changed them to see if I had found the point where the view is called, but despite changing the file name on these entries - the app still works when I visit the URL: http://foofoofoo.local/protected/admin/org/edit/1
I just want to see where the admin_edit.ctp file is being called within the site.
URL: http://foofoofoo.local/protected/admin/org/edit/1
This means I can assume you have a added a route in your /app/Config/routes.php. Where this is pointing can not be said since we don't have access to this file.
Why can I assume you have added this to your routes? Because the posted URL is not matching the CakePHP Conventions which clearly states that controllers should be defined in plural. Since the URL will be accessing the Controller directly through the Controller, unless a route has been specified, I know that the OrgController does not exist. Why?
Try Inflector::pluralize('Org'). It will return 'Orgs' to you. And thus meaning the controller should be called OrgsController and you should be accessing this Controller via the following URL.
http://foofoofoo.local/protected/admin/orgs/edit/1
In this OrgsController there should be an action (function) called admin_edit(), because you have prepended the org with Admin, which is a prefix.
It can be possible that the /protected part, is part of the URL as well, but do not know where your main /App is located and what part of the URL is pointing to the /app/webroot/index.php file.
The Views can be found at /app/View/Orgs/*.ctp.
If you are still having trouble finding your files. Please start with the Blog tutorial written by the Cake Community. This tutorial describes all the neat built-in tricks and will get your first app running in no-time. Please read that first!
If you are still having trouble, feel free to update your question and add the /app/Config/routes.php file.
Under Cake 1.3, if your application has an AppController (check if the file app/app_controller.php exists), you can put this code in the beforeFilter method:
debug($this->params);
It will print an array on your app pages when you are in debug mode, with the name of the controller and the action used.
Array
(
...
[controller] => controller_name
[action] => action_name
...
)
If the AppController does not contain any beforeFilter method, you can just create it:
function beforeFilter()
{
debug($this->params);
}

HTMLHelper addCrumb method not available

I would like to use the addCrumb method in my Layout to automatically add controller links. I tried this but the Html-Helper-Object in the Layout didn't contain the addCrumb Function. Then I tried to use the function in the beforeFilter in my AppController to set the Link but this wont work too (no error given). At last I tried to use an element to make this happen, but this didnt the job (error method not found).
I am using CakePHP 2.0 - has anybody an idea to solve my problem (without changing the *.ctp files by hand)?
PS: Using $this->html->addCrumb() in my specific .ctp-file works great.
To have access to HTML helper methods such as addCrumb, you must make sure the helper is loaded for whatever action you want to use it in. Simply do $this->helpers[] = 'Html'; in your controller (in an action or in your AppController to add it universally).

method specific authentication in Phil Sturgeons php codeigniter rest api

Hi
I know that I can set the rest authentication in Phil Sturgeons rest API, but I only want authentication for some methods in the REST API.
I want some of my methods to be accessible for everyone with no authentication at all, and others to only be accessible to administrators/people authenticated users.
In .net I can simply set a [RequiresAuthentication] attribute over methods in a webservice, is there something similar I can do with Rest PHP in CodeIgniter?
Or Controller specific would be fine too.
"philsturgeon Phil Sturgeon
Why do people ask questions about my code on StackOverflow and random forums instead of just asking me?"
Go ask Phil Sturgeon.
Hello Jakob :) What you are trying to do is a bit tricky as Phil Sturgeons rest API Controller only supports setting the authentication method globally. To set it globaly you edit this line in the rest config file:
$config['rest_auth'] = '';
I have an untested theory though:
To set this setting per controller make sure the setting in the config file is as above (empty) and add this constructor to the controller you would like to specify authentication method for:
function __construct()
{
$this->load->config('rest');
//$this->_prepare_basic_auth(); //Uncomment to use basic
//$this->_prepare_digest_auth(); //Uncomment to use digest
parent::Controller();
}

Categories