Calling module controller function from page formulary in CodeIgniter 4.1 HMVC - forms

I'm working in an application HMVC CodeIgniter 4.1, all the routes work fine in navigator:
Navigator adress: http://localhost/myapp/public/index.php/installation/shop-data
My module Routes file contains:
$routes->group("installation", ["namespace" => "\Modules\Installation\Controllers"], function ($routes) {
//Example URL: /installation/shop-data
$routes->get("shop-data", "InstallationController::shop_data");
$routes->get("shop-data-post", "InstallationController::shop_data_post");
});
shop_data_post is the function in which I insert the data to the database from the page Form using this:
echo form_open("installation/shop-data-post");
But I obtain this 404 error:
Controller or its method is not found: \App\Controllers\Installation::shop-data-post
How can I get my function correctly from my page formulary?
Thanks

Ok finally I have solved this..
All that I must did was change my module Installation Routes file so:
This line:
$routes->get("shop-data-post", "InstallationController::shop_data_post");
For this:
$routes->match(["get", "post"], "shop-data-post", "InstallationController::shop_data_post");
The error was on the route request method. It must be POST for process the Form data

Related

Sitemap for dynamic website - codeigniter

I have a dynamic website designed with Codeigniter 3 and I am working on the sitemap part as a newbie.
I found the library sitemap-php from evert/sitemap-php but I can't make it run.
From now this is what I did, I put the Sitemap.php file into my library folder
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Deals extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url', 'form', 'security');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('email');
$this->load->model('deal_model');
$this->load->helper(array('cookie','custom','text'));
}
public function Sitemap(){
$this->load->library('Sitemap');
$sitemap = new Sitemap('https://www.mywebsite.com');
$sitemap->setPath('/public_html/Sitemap/'); // I created a folder Sitemap into my public folder
$sitemap->setFilename('sitemap');
$sitemap->addItem('/', '1.0', 'daily', 'Today');
$sitemap->createSitemapIndex('https://www.mywebsite.com/sitemap/', 'Today');
}
Then when I go to https://www.mywebsite.com/sitemap/, I have an Error 404.
Could you guide me to solve my issue.
Thanks
The docs for that library describe that it generates a static XML file. The code you've shown will do that - but your code is in a Library, and you have not run it yet. You need to run it, then it will generate an XML file as you've specified, in /public_html/Sitemap/. From your description you are looking for the XML before doing anything to generate it, and it does not (yet) exist.
From your updated code, you now have the code to generate the static XML available as a Controller method. According to the standard Codeigniter routing conventions, the method you have created is accessible at:
http://your-site/deals/Sitemap
(Maybe you've also set up some routes so it is accessible at other URIs also.)
Visit that URL, once, to generate the static XML file at /public_html/Sitemap/sitemap.xml. Assuming your code works, you should then be able to browse the XML at
http://your-site/Sitemap/sitemap.xml
Side note: AFAIK Codeigniter convention is for capitalised Controller file and class names (Deal.php and Deal), but all lower-case method names (sitemap() instead of Sitemap()). You can see examples of this in the Controller docs I linked above. I am not sure if it matters, just pointing it out.

Creating a REST route like /users/new

Is it possible to create a REST endpoint in play like:
/users/new
That would also call a method named new ?
I tried doing this like:
def `new`:= {
...
}
The action worked fine, but the line in my route file was not working because of the new keyword:
GET /users/new #controllers.UserController.new
I like following the rubyonrails convention with URL endpoints.
I am not a expert but try this:
GET /users/new #controllers.UserController.new()
Also your function new() is under UserController class?
new is reserved word in Java at least. I made successfully controller:
GET /user/new controllers.UserController.news()

Play form binding difficulties - How do I modify a request after getting it in the backend?

I'm currently trying to connect an Extjs form to a Play! Scala backend.
I have no problem receiving the request, but
FormName.bindFromRequest.get returns to me None .
I'm able to find the data generated via submitting from Extjs in the POST request in request.request.body.data (the first request object is generated by SocialSecure's controller), whereas normally the data bound to the form would be found in request.request.body.data.elems
I think Extjs's eschewal of using <form> in their inserted HTML is what causes me this problem, but I'd still like to take advantage of Extjs's nice form verification UI.
Does Play! or Scala have any resources for modifying a request after the server has received it?
More info
This is the method my /requestAudit cuurently points to after a POST request:
def requestAudit = SecuredAction(WithProvider("google")) { // SecureSocial syntax
implicit request => { // let's call this line 0'
println(request.request.body.asFormUrlEncoded) // let's call this line 1'
println(request.body.asText) // let's call this line 2'
newAuditForm.bindFromRequest.fold(
errors => BadRequest(views.html.error(newAuditForm))
success => { /*insert the object into my db*/ }
) } }
Ext.js request
When I'm debugging in Eclipse with an Ext.js form, the Variables window shows: (click for closeup)
where the form values are located in request.body.data.key1, request.body.data.key2, etc
Bootstrap form request
On the other hand, the request for Bootstrap has the values stored in request.body.data.elems
#2manyprojects 's suggestion set me on the right path:
newAuditForm.bindFromRequest(
(request.request.body.asFormUrlEncoded).getOrElse(Map()))
.fold( ... )
worked.
I was still getting form binding errors after changing my code to this, and then I discovered a typo in the name property of one of my Ext.js form fields. The name of the field must be the same on both the UI and the Play Form.

Call to undefined function bp_core_get_user_email() - Buddypress issue

I am having a issue where I am trying to retrieve the email of the user who is logged in using Buddypress. Here is my code:
global $bp;
echo bp_core_get_user_email($bp->loggedin_user->id);
Here is the error message that pops up when I open the php page:
"Fatal error: Call to undefined function bp_core_get_user_email() in /home/user/public_html/useremail.php on line 4"
Have you loaded WordPress & BuddyPress in your file useremail.php?
I see that it's the same level as wp-config.php. To make it know anything about WP/BP functions you need to do at least this:
include ('./wp-load.php');
Otherwise in your situation that php file will through errors everytime you will use non-standard php functions.
But the true way is to use WP - create:
1) a plugin that will handle all requests to a specific url
OR
2) create a page in WP dahsboard with a specific page template, and in its template file you can write whatever code you need or want.
Other option to get the email:
$user_active = wp_get_current_user();
$user_mail = $user_active->user_email;

404 Page Not Found With CodeIgniter REST_Controller

I'm trying to use CodeIgniter REST_Controller. Following is my code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once (APPPATH.'/libraries/REST_Controller.php');
class Test extends REST_Controller{
public function index_get() {
$this->response(array(''));
}
}
When I access http://codeigniter/index.php/test the response
<xml><item/></xml>
is shown on the browser.
But when trying to access
http://codeigniter/index.php/test.xml or http://codeigniter/index.php/test.html I get a 404 error.
The documentation of the above mentioned library says that both
http://example.com/books.json
http://example.com/books?format=json
works with the library.
Can someone point out to me what I'm doing wrong?
You must call it like this
SERVER-URL/index.php/test/index
And if you have the test.php inside a folder in controllers then call it like this
SERVER-URL/index.php/test/FOLDER-NAME/index
Tahir Rauf
http://www.onethatmatters.com