Codeigniter Version 4.1.7 Rest API Routing Post method not working - codeigniter-4

I am using Codeigniter Version 4.1.7.
Where I am implementing Rest API.
In the routes the GET method works,however Post method is not working.
I am testing this using POSTMAN.
URL : http://localhost/myproject/api/add-user
Following is the header
Accept: application/json
Content-Type: application/json
Authorization: Basic xxxxxxxxxxx=
Please check the code below for reference.
Routes.php
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(false);
/*
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/
// We get a performance increase by specifying the default
// route since we don't have to scan directories.
//$routes->get('/', 'Home::index');
$routes->group("api", ["namespace" => "App\Controllers\Api", "filter" => "basicauth"] , function($routes){
$routes->get("list-users", "ApiController::index");
$routes->post("add-user", "ApiController::create");
});
ApiController.php
app\Controllers\Api\ApiController.php
<?php
namespace App\Controllers\Api;
use CodeIgniter\RESTful\ResourceController;
use App\Models\UsersModel;
class ApiController extends ResourceController
{
/**
* Return an array of resource objects, themselves in array format
*
* #return mixed
*/
public function index()
{
//
$users = new UsersModel();
$response = [
'status' => 200,
"error" => false,
'messages' => 'User list API',
'data' => $users->findAll()
];
return $this->respondCreated($response);
}
/**
* Create a new resource object, from "posted" parameters
*
* #return mixed
*/
public function create()
{
//
$rules = [
'first_name' => 'required|min_length[3]|max_length[20]',
'last_name' => 'required|min_length[3]|max_length[20]',
'email' => 'required|min_length[6]|max_length[50]|valid_email|is_unique[users.email]',
'password' => 'required|min_length[8]|max_length[255]',
'password_confirm' => 'matches[password]',
];
...
...
...
return $this->respondCreated($response);
}
}
Any help would be appreciated.

Related

MS Graph API returns 500 when I try to request groups

So I am developing a sharepoint webpart where I need to check if a user is in an AD group. Now here is the odd part the MS Graph query I need to you for that is:
https://graph.microsoft.com/v1.0/me/transitiveMemberOf/microsoft.graph.group?$count=true
And that is the exact Query that my WP sends out, but it returns a 500 error message. Now I thought I had permissions missing, but the Explorer says I do not need any.
Here is my GraphService that handles MS Graph:
import { MSGraphClient } from '#microsoft/sp-http';
import * as MicrosoftGraph from '#microsoft/microsoft-graph-types';
import { WebPartContext } from "#microsoft/sp-webpart-base";
/**
* The class that handles the MS Graph API calls
*/
export default class GraphService {
/**
* The MS Graph client with does the calls
*/
private _client:MSGraphClient;
/**
* Sets the client for the Graph API, needs to be called before the class can be used
* #param context The context of the WP
*/
public async setClient(context:WebPartContext) {
this._client = await context.msGraphClientFactory.getClient();
}
/**
* Checks to see if a user belongs in a user group or not
* #param groupName The group name with we want to check
*/
public async checkCurrentUserGroup(groupName:string) {
const rawData = await this._client.api('/me/transitiveMemberOf/microsoft.graph.group').count(true).get();
const arr = rawData.value.filter(i => i.displayName == groupName);
return !!arr.length;
}
/**
* Returns the users from AD who have a manager and whom companyName is a specific company name
* #param companyName The company name to whom the users need to belong to
*/
public async getUsers(companyName:string):Promise<any[]> {
const rawData = await this._client.api('/users').filter('accountEnabled eq true').expand('manager').select('displayName,companyName,mail,department,jobTitle').get();
//need to do manual filtering, becuase you can't user filter on the "companyName" field and you can't search and expand at the same time without returing everything
const filteredData = rawData.value.filter(i => i.companyName == companyName && i.manager);
//remaps the data to IUserModel
const ret:any[] = filteredData.map(i => <any>{
name: i.displayName,
id: 0,
email: i.mail,
manager: i.manager.displayName,
managerEmail: i.manager.mail,
managerId: 0,
hasDiscussionForm: false,
department: i.department,
position: i.jobTitle
});
return ret;
}
}
The problem then is that the method checkCurrentUserGroup returns 500.
I've given the following permissions to the wp:
Group.Read.All
Directory.Read.All
User.Read
The getUsers method works as expected.
In the MS Graph Explorer the query works just fine. What am I missing?
According to the document the request header must have ConsistencyLevel and eventual header and please make sure to pass the request header and please refer to this document for more information
let res = await client.api('/users/{id}/transitiveMemberOf/microsoft.graph.group')
.header('ConsistencyLevel','eventual')
.search('displayName:tier')
.select('displayName,id')
.orderby('displayName ')
.get();

Problems with Shopware snippets

Greeting everyone !
I have a problem - snippets not works.
In Theme.php I use next
/**
* #param Form\Container\TabContainer $container
*/
public function createConfig(Form\Container\TabContainer $container)
{
$container->addTab($this->createBasicTab());
}
/**
* Create Basic Tab
* #return Form\Container\Tab
*/
public function createBasicTab()
{
$tab = $this->createTab(
'basic_settings',
'__basic_settings__',
[
'attributes' => [
'layout' => 'anchor',
'autoScroll' => true,
'padding' => '0',
],
]
);
In snippet located in _private/snippets/backend/config.ini I used next
[en_GB]
basic_settings = 'Basic settings'
[de_DE]
basic_settings = 'Basic settings de'
And in admin part I got next
Please, help!(
You can use it like this:
Shopware()->Snippets()->getNamespace('backend/config')->('__basic_settings__', 'There you can put value by deault')
Documentation for snippets.
In theme manager we have checkbox force snippet reload in settings. Please, enable it. This will save your nerves

How to implement a pagination for a search module in Zend Framework 2?

I have a module Search in my ZF2 application. The user fills in a search form out and gets a list of courses.
Now I'm adding the pagination to the module. The paginator is basically working: I can retrieve data over it and the pagination is displayed correctly (pagelinks 1-7 for 70 found courses with the dafault setting 10 items per page).
But it's still not usable. When I click on a pagelink, the form POST data is lost. I know -- it cannot work the way, how I implemented it (see the code below). But I have no idea, how to do it correctly, in order to eep checking the form data and nonetheless be able to use pagination.
That is my code:
Table class Search\Model\CourseTable
class CourseTable {
...
// without pagination
// public function findAllByCriteria(CourseSearchInput $input) {
// with pagination
public function findAllByCriteria(CourseSearchInput $input, $pageNumber) {
...
$select = new Select();
$where = new Where();
$having = new Having();
...
// without pagination
// $resultSet = $this->tableGateway->selectWith($select);
// return $resultSet;
// with pagination
$adapter = new \MyNamespqce\Paginator\Adapter\DbSelect($select, $this->tableGateway->getAdapter());
$paginator = new \Zend\Paginator\Paginator($adapter);
$paginator->setCurrentPageNumber($pageNumber);
return $paginator;
}
...
}
Search\Controller\SearchController
class SearchController extends AbstractActionController {
public function searchCoursesAction() {
$form = $this->getServiceLocator()->get('Search\Form\CourseSearchForm');
$request = $this->getRequest();
if ($request->isPost()) {
$courseSearchInput = new CourseSearchInput();
$form->setInputFilter($courseSearchInput->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$courseSearchInput->exchangeArray($form->getData());
// without pagination
// $courses = $this->getCourseTable()->findAllByCriteria($courseSearchInput);
// with pagination
$page = $this->params()->fromRoute('page');
$paginator = $this->getCourseTable()->findAllByCriteria($courseSearchInput, $page);
} else {
$paginator = null;
}
} else {
$paginator = null;
}
return new ViewModel(array(
'form' => $form,
// without pagination
// 'courses' => $courses,
// with pagination
'paginator' => $paginator,
'cities' => ...
));
}
...
}
How to get it working?
I also have the same problem, and I have solved it. But this is not good way. May be the idea will help you.
I solved it as follow: (Search pagination for Zend tutorial album module)
I build two action in controller named "search" and "index".
Whenever the search form submitted, it always post the value to search action. Search action build the url with search parameters, and redirect to index to disply search result.
And when the pagination links clicked, then posted values are passed through url. So whenever index action ask for search parameters, it always get the values in same format.
I defined route as follows:
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id][/page/:page][/order_by/:order_by][/:order][/search_by/:search_by]',
'constraints' => array(
'action' => '(?!\bpage\b)(?!\border_by\b)(?!\bsearch_by\b)[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
'page' => '[0-9]+',
'order_by' => '[a-zA-Z][a-zA-Z0-9_-]*',
'order' => 'ASC|DESC',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
There is a parameter named "search_by", which will keep all search parameters as a json string. This is the point, which is not good I know, but have not find any other way yet.
"Search" action build this string as -
public function searchAction()
{
$request = $this->getRequest();
$url = 'index';
if ($request->isPost()) {
$formdata = (array) $request->getPost();
$search_data = array();
foreach ($formdata as $key => $value) {
if ($key != 'submit') {
if (!empty($value)) {
$search_data[$key] = $value;
}
}
}
if (!empty($search_data)) {
$search_by = json_encode($search_data);
$url .= '/search_by/' . $search_by;
}
}
$this->redirect()->toUrl($url);
}
And next index action decode the string, do necessary action, and also send the json string to view.
public function indexAction() {
$searchform = new AlbumSearchForm();
$searchform->get('submit')->setValue('Search');
$select = new Select();
$order_by = $this->params()->fromRoute('order_by') ?
$this->params()->fromRoute('order_by') : 'id';
$order = $this->params()->fromRoute('order') ?
$this->params()->fromRoute('order') : Select::ORDER_ASCENDING;
$page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;
$select->order($order_by . ' ' . $order);
$search_by = $this->params()->fromRoute('search_by') ?
$this->params()->fromRoute('search_by') : '';
$where = new \Zend\Db\Sql\Where();
$formdata = array();
if (!empty($search_by)) {
$formdata = (array) json_decode($search_by);
if (!empty($formdata['artist'])) {
$where->addPredicate(
new \Zend\Db\Sql\Predicate\Like('artist', '%' . $formdata['artist'] . '%')
);
}
if (!empty($formdata['title'])) {
$where->addPredicate(
new \Zend\Db\Sql\Predicate\Like('title', '%' . $formdata['title'] . '%')
);
}
}
if (!empty($where)) {
$select->where($where);
}
$album = $this->getAlbumTable()->fetchAll($select);
$totalRecord = $album->count();
$itemsPerPage = 2;
$album->current();
$paginator = new Paginator(new paginatorIterator($album));
$paginator->setCurrentPageNumber($page)
->setItemCountPerPage($itemsPerPage)
->setPageRange(7);
$searchform->setData($formdata);
return new ViewModel(array(
'search_by' => $search_by,
'order_by' => $order_by,
'order' => $order,
'page' => $page,
'paginator' => $paginator,
'pageAction' => 'album',
'form' => $searchform,
'totalRecord' => $totalRecord
));
}
All the sorting and paging url contain that string.
If you know all the searching paarameters before, then you can define that at route, and pass like the same way without json string. As I have to build a common search, I have build a single string.
Source code for "Album search" is available in git hub at https://github.com/tahmina8765/zf2_search_with_pagination_example.
Live Demo: http://zf2pagination.lifencolor.com/public/album
#Sam & #automatix in the question comments are both right. My suggestion (though I'm looking for a simpler alternative) is to construct a segment route, which covers all of the options that you're likely to need and start with a standard form POST request.
Then, after the request is validated, pass the form data to the paginationControl helper as follows:
$resultsView = new ViewModel(array(
'paginator' => $paginator,
'routeParams' => array_filter($form->getData())
));
Then, in your view template, set the route parameters in the paginationControl view helper:
<?php echo $this->paginationControl($paginator, 'Sliding', 'paginator/default',
array('routeParams' => $routeParams)
) ?>
I've used array_filter here because it's a really simple way of removing any element from the form data that's null, empty or so on. That way you don't pass in extra data that you don't need.

Symfony2 Entity form type with a specific query_buider

Context
In my case, I've some orders with "discount vouchers" (discount). A discount can be use on under different conditions. For instance, discounts have an expired date, can be used by a limited number of customers, can be dedicated to a user, ...
Each discount can be attached to several order.
In my backoffice, I want to add to order create form a field "Discount" with a list of discount available but only right discounts.
What I made
An entity "order" with a field manyToMany
/**
* #ORM\ManyToMany(targetEntity="PATH\MyBundle\Entity\Discount", inversedBy="orders")
* #ORM\JoinTable(name="shop_discounts_orders",
* joinColumns={#ORM\JoinColumn(name="order_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="discount_id", referencedColumnName="id")}
* )
*/
private $discounts;
An entity "discounts" with a field manyToMany
/**
* #ORM\ManyToMany(targetEntity="PATH\MyBundle\Entity\Order", mappedBy="discounts")
*/
private $orders;
A form OrderType with a field discounts
$builder->add('discounts', 'entity',
array( 'label' => 'Discount vouchers',
'required' => false,
'expanded' => true,
'class' => 'PATH\MyBundle\Entity\Discount',
'property' => 'title',
'multiple' => true,
'query_builder' => function(EntityRepository $er) use ($params) {
return $er->getQuerySelectType($params);
},
));
With this solution, I can return specific discount defined by my request in my entity repository. It's good for expired date condition for instance.
What I would like
I'd like to filter results in the checkbox list. In fact, I want limit usage of the discount to a dedicated user, limit to a list of products, or limit the number of usage... And these condition cannot be done by a simple sql request.
I try to create special Type. My idea is to have an array of entities Discount and load a choice list... After that, I create a dataTransformer but It doesn't work !
Thank's for your ideas !
You could use the $options from public function buildForm(FormBuilderInterface $builder, array $options) to pass your user and product for instance. With those 2 informations you could refine your list of discount (in your query)
if you do so you need to add them in the setDefaultValue
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'user_discount' => null,
'product_discount' => null,
));
}
and in your controller:
$form = $this->formFactory->create(new YourFormType(), $entity, array(
'user_discount' => $this->getUser(),
'product_discount' => $product,
));
I found a solution and explain it if someone have the same issue as me.
Create a custom Type
My custom type is inspired by Symfony\Bridge\Doctrine\Form\Type\DoctrineType
class DiscountOrderType extends AbstractType
{
// overide choiceList callback
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$choiceListCache =& $this->choiceListCache;
$type = $this;
$choiceList = function (Options $options) use (&$choiceListCache, &$time, $container) {
[[ Copy paste same as Doctrine type ]]
// Create your own choiceList class (EntityChoiceList)
if (!isset($choiceListCache[$hash])) {
$choiceListCache[$hash] = new DiscountChoiceList(
$options['em'],
$options['class'],
$options['property'],
$options['loader'],
$options['choices'],
$options['group_by']
);
// If you want add container
$choiceListCache[$hash]->setContainer($container);
}
return $choiceListCache[$hash];
};
$resolver->setDefaults(array(
'choice_list' => $choiceList,
));
}
Create a custom EntityChoiceList
My custom type is inspired by Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList
class EntityChoiceList extends ObjectChoiceList
{
protected function load()
{
if ($this->entityLoader) {
$entities = $this->entityLoader->getEntities();
} else {
$entities = $this->em->getRepository($this->class)->findAll();
}
// You have access to the entities in the choice list
// Add your custom code here to manipulate the choice list
// you can do some check not properly possible with sql request (http requests on each result, ...) before add it in choice list
// you can add some custom cache rules, ...
// if you use gedmon and want apply a "join" with translate table, you can add $query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'); before playing request...
// Possibilities are infinite
// FOR INSTANCE : you already want unset first entity of the result
if (isset($entities[0])) {
unset($entities[0]);
}
// END OF CUSTOM CODE
try {
// The second parameter $labels is ignored by ObjectChoiceList
// The third parameter $preferredChoices is currently not supported
parent::initialize($entities, array(), array());
} catch (StringCastException $e) {
throw new StringCastException(str_replace('argument $labelPath', 'option "property"', $e->getMessage()), null, $e);
}
$this->loaded = true;
}
Of course you can try to extend symfony class for beautyfull code ;).
Thank's to #maxwell2022 for your help !

Kohana 3 - Get URLs

could you help me with following questions.
How do i get the:
absolute/relative current url
http://www.example.com/subdir/controller/action
/subdir/controller/action
absolute/relative application url
http://www.example.com/subdir/
/subdir/
I could of course use native php to get it but i think i should rather use ko3 functions.
Any idea how that works?
Thanks in advance!
Tried to make a controller that outputted them all correctly. Let me know if any of them are off.
class Controller_Info extends Controller
{
public function action_index()
{
$uris = array
(
'page' => array
(
'a' => Request::instance()->uri(),
'b' => URL::base(TRUE, FALSE).Request::instance()->uri(),
'c' => URL::site(Request::instance()->uri()),
'd' => URL::site(Request::instance()->uri(), TRUE),
),
'application' => array
(
'a' => URL::base(),
'b' => URL::base(TRUE, TRUE),
'c' => URL::site(),
'd' => URL::site(NULL, TRUE),
),
);
$this->request->headers['Content-Type'] = 'text/plain';
$this->request->response = print_r($uris, true);
}
public function action_version()
{
$this->request->response = 'Kohana version: '.Kohana::VERSION;
}
public function action_php()
{
phpinfo();
}
}
Outputs this:
Array
(
[page] => Array
(
[a] => info/index
[b] => /kohana/info/index
[c] => /kohana/info/index
[d] => http://localhost/kohana/info/index
)
[application] => Array
(
[a] => /kohana/
[b] => http://localhost/kohana/
[c] => /kohana/
[d] => http://localhost/kohana/
)
)
Technically speaking, it's actually only the first page url that is a real relative url, since all the others either start with / or http://.
Needed to get the url for the current page myself, so decided to extend the url class. Thought I could share it here. Let me know what you think :)
/**
* Extension of the Kohana URL helper class.
*/
class URL extends Kohana_URL
{
/**
* Fetches the URL to the current request uri.
*
* #param bool make absolute url
* #param bool add protocol and domain (ignored if relative url)
* #return string
*/
public static function current($absolute = FALSE, $protocol = FALSE)
{
$url = Request::instance()->uri();
if($absolute === TRUE)
$url = self::site($url, $protocol);
return $url;
}
}
echo URL::current(); // controller/action
echo URL::current(TRUE); // /base_url/controller/action
echo URL::current(TRUE, TRUE); // http://domain/base_url/controller/action
Don't you just mean:
Kohana_Request::detect_uri() ?
Absolute/Relative current URL:
// outputs 'http://www.example.com/subdir/controller/action'
echo URL::site(Request::detect_uri(),true));
// outputs '/subdir/controller/action'
echo URL::site(Request::detect_uri());
Absolute/Relative current application URL:
// outputs 'http://www.example.com/subdir/'
echo URL::site(NULL, TRUE);
// outputs '/subdir/'
echo URL::site();
Hope it helps

Resources