Can't use post method on REST_API Codeigniter - rest

This is my code:
require APPPATH . '/libraries/REST_Controller.php';
class Login extends REST_Controller
{
function __construct($config = 'rest')
{
parent::__construct($config);
}
public function index_post()
{
echo "Post";
}
public function index_get()
{
echo "Get";
}
}
But I use Postman to test:
When I use Post method:
When I use Get method
I use different method, but it work only with index_get. If I remove function index_get and use post method on Postman, it will be unknow method.
How to fix it? I need use post method.

you are using the same function name, change the function name. It might be different(_post,_get) while you are coding, But when you are accessing through url you have to remove the method(_post,_get) which means both are of same function name. So it returns the first function return value

Related

Proper way to access the container from a class not in a slim controller

I have a regular php class outside of a controller, so it doesn't benefit from automatic injection of container. I need to access the response object from that class, and I guess I should get it from the container.
What's the proper way to access it ? Just pass it as argument so the outside class can use it ? Is there a better way ?
You need to use middleware for that because the response object is immutable so "changing" it will not update the response which will be used by slim.
$app->add(function($request, $response, $next) {
if($shouldRedirect === true) {
return $response->withRedirect('myurl'); // do not execute next middleware/route and redirect
}
return $next($request, $response); // execute next middleware/ the route
});
For more information about middleware have a look at this.
If you need to send a subrequest, Slim provides such functionality. Use it carefully though, as in some situations its result is not obvious.
<?php
class MySortOfOutsideClass
{
/**
* If you need to send a subrequest, you have to access application instance,
* so let's inject it here.
*/
public function __construct(\Slim\App $app)
{
$this->$app = $app;
}
/**
* Method that makes a subrequest, and returns the result of it.
*/
public function myMethod()
{
if ($subRequestIsRequired) {
return $this->app->subRequest('GET', '/hello');
}
}
}

Lumen inconsistent behaviour with GET request

For an API I am writing, in my routes file I have:
$app->get('item/{id}', 'ApiController#item');
$app->get('groupitems/{group}', 'ApiController#groupItems');
In my Controller I have the relevant two functions:
public function item($id, Request $request)
{
if ($this->isAuthorised($request->input('tenant_id'), $request->input('api_code'))) {
$item = Line::find($id);
if ($item) { ...
public function groupItems($id, Request $request)
{
if ($this->isAuthorised($request->input('tenant_id'), $request->input('api_code'))) {
$items = Line::where('tenant_id', $request->input('tenant_id'))->where('publish', true) ...
The calls are both made in exactly the same way, for example:
http://api.artlook.com/groupitems/29?tenant_id=2&api_code=o9rty43
Please don't try that as the URL is only on a local server at the moment.
My first function runs perfectly. The second one returns an error exception
Argument 2 passed to groupItems() must be an instance of Illuminate\Http\Request, string given
But they are identical and in the same controller. Help?
The problem here, is that you have declared the parameter name as {group}. Therefore, you need to set the argument variable to that name:
public function groupItems($group, Request $request)
...

Zend framework Plugin Authenticate

I'm trying to create a ZF1 plugin to centralize my Authentication system. So far here is what I did :
class Application_Plugin_Auth extends Zend_Controller_Plugin_Abstract {
private $_whitelist;
protected $_request;
public function __construct() {
$this->_whitelist = array(
'default'
);
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$this->_request = $request;
$module = strtolower($this->_request->getModuleName());
if (in_array($module, $this->_whitelist)) {
return;
}
$auth = Zend_Auth::getInstance();
if (!$auth->hasIdentity()) {
$this->_request->setModuleName('admin');
$this->_request->setControllerName('auth');
$this->_request->setActionName('login');
return;
}
}
}
It works perfectly to avoid people to access the backend if there are not logged. Now, I would like to implement a login function with no parameters which will grab the current request, check the param (getPost) and then do the login job :
public function login(){
// Here will check the request data and then try to login
}
My question is how can I get the current request object in this function? Also, how to use this login function in my controller?
Thanks a lot
This is what you want when you don't want to pass the request as argument to your function:
$request = Zend_Controller_FrontController::getInstance()->getRequest();
$postData = $request->getPost();
However, usually you do want to pass arguments to your function. Mostly because you want your object that operates with the login functionality to be independent from the rest of your system. There are only few cases I can think of that disagree from this methodology.
When you like to get the Request from your front controller, you can just issue:
$request = $this->getRequest();

ZF2 Use Redirect in outside of controller

I'm working on an ACL which is called in Module.php and attached to the bootstrap.
Obviously the ACL restricts access to certain areas of the site, which brings the need for redirects. However, when trying to use the controller plugin for redirects it doesn't work as the plugin appears to require a controller.
What's the best way to redirect outside from outside of a controller? The vanilla header() function is not suitable as I need to use defined routes.
Any help would be great!
Cheers-
In general, you want to short-circuit the dispatch process by returning a response. During route or dispatch you can return a response to stop the usual code flow stop and directly finish the result. In case of an ACL check it is very likely you want to return that response early and redirect to the user's login page.
You either construct the response in the controller or you check the plugin's return value and redirect when it's a response. Notice the second method is like how the PRG plugin works.
An example of the first method:
use Zend\Mvc\Controller\AbstractActionController;
class MyController extends AbstractActionController
{
public function fooAction()
{
if (!$this->aclAllowsAccess()) {
// Use redirect plugin to redirect
return $this->redirect('user/login');
}
// normal code flow
}
}
An example like the PRG plugin works:
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Http\Response;
class MyController extends AbstractActionController
{
public function fooAction()
{
$result = $this->aclCheck();
if ($result instanceof Response) {
// Use return value to short-circuit
return $result
}
// normal code flow
}
}
The plugin could then look like this (in the second case):
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
class AclCheck extends AbstractPlugin
{
public function __invoke()
{
// Check the ACL
if (false === $result) {
$controller = $this->getController();
$redirector = $controller->getPluginManager()->get('Redirect');
$response = $redirector->toRoute('user/login');
return $response;
}
}
}
In your question you say:
[...] it doesn't work as the plugin appears to require a controller.
This can be a problem inside the controller plugin when you want to do $this->getController() in the plugin. You either must extend Zend\Mvc\Controller\Plugin\AbstractPlugin or implement Zend\Mvc\Controller\Plugin\PluginInterface to make sure your ACL plugin is injected with the controller.
If you do not want this, there is an alternative you directly return a response you create yourself. It is a bit less flexible and you create a response object while there is already a response object (causing possible conflicts with both responses), but the plugin code would change like this:
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Http\PhpEnvironment\Response;
class AclCheck extends AbstractPlugin
{
public function __invoke()
{
// Check the ACL
if (false === $result) {
$response = new Response;
$response->setStatusCode(302);
$response->getHeaders()
->addHeaderLine('Location', '/user/login');
return $response;
}
}
}

zend-framework, call an action helper from within another action helper

i am writing an action helper and i need to call another action helper from within that helper. but i dont know how. here in the sample code:
class Common_Controller_Action_Helper_SAMPLE extends Zend_Controller_Action_Helper_Abstract
{
protected $_view;
public function __construct(Zend_View_Interface $view = null, array $options = array())
{
$this->_view = $view;
}
public function preDispatch()
{
$flashMessenger = $this->_helper->FlashMessenger; // IT IS NULL
}
}
Use the action helper broker:
$flashMessenger =
Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
You can also use getActionController to get a reference back to the actioncontroller you were using for any methods you'd normally use there.
In addition to mercator's answer, add your method after, see example below:
Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger')->myMethod();
You can call it in this way:
$this->_actionController->OtherActionHelper();
The _actionController property references the actual action controller.