How to make a pretty simple Codeigniter url route? - codeigniter

I've looked some websites have a simple route in their login/registration form. For instance, their form redirect to action="/function/method". Recenttly I want to implement that route in codeigniter (I use codeigniter 3), but my form still break. I don't know how to.
Below is my simple controller:
/*
* i.e Folder: Login
* i.e Controller name: Login
* i.e steps: login/login/verify
*/
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
// Load login view
}
public function verify()
{
// Processing data from login form
}
}
And the login form is:
<form action="<?php echo base_url('login/login/verify'); ?>" method="post" class="form-horizontal">
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
<button class="btn btn-login" type="submit">Sign in</button></p>
</form>
As we can see the action is redirect to base_url()/login/login/verify or if we print this will make http://localhost/domain/login/login/verify. I think it's seem too long. What I want to is to simplify that route, for instance, action="/login/verify". I try to make route $route['login/(:any)'] = "login/login/$1" and it's not working. A little trick maybe to rename login folder i.e user, so that will be user/login/verify. But it's not what I want to.
Any idea or direction?

The issue is a you're attempting to call a method that doesn't exit.
Remember that Codeigniter's URL Scheme is:
http://localhost/codeigniter_installation/controller/method/params/..../
From your question, you appear to be calling:
/login/login/verify
login - controller
login - method
verify - param
This would be listed as: function login($action = "verify") { do_stuff() } in your controller.
When you should just be calling: /login/verify

Related

Fail to retrieve Post data with Laravel

I try to learn Laravel (v.9) from scratch but fail to do a simple basic task:
I just want to send "post_var" by POST request and display the var.
My form (blade template):
<form action="/post_and_show" method="POST">
#csrf
#method('post')
<input type="text" name="post_var">
<input type="submit" value="send">
</form>
My Route (in web.php) with some dd() i tried to find the problem
Route::post('/post_and_show', function (Request $request) {
dd($request->method()); // returns GET ?? !!
// chrome debugger network tab show clearly its a POST request...
dd($request->all()); // returns empty Array
dd($request->post_var); // returns null
dd($request->getContent()); // returns string but i want param only
// "_token=yBBYpQ303a1tSiGtQF6zFCF6p6S7qadVfHMk4W7Q&_method=post&post_var=12345"
});
What am i doing wrong here?
Tried several methods i found in the documentation but non worked so far.
EDIT: I removed "#method('post')"
Btw.: My initial version that did not work either had no "#method('post')". I added it later on in the hope it might help...
<form action="/post_and_show" method="POST">
#csrf
<input type="text" name="post_var">
<input type="submit" value="send">
</form>
But have still the same problems:
Route::post('/post_and_show', function (Request $request) {
dd($request->method()); // returns GET ?? !! but chrome says its a post request
dd($request->post_var); // returns null
dd($request->get('post_var'));// returns null
dd($request->getContent()); // returns complete body and header in one string
dd($request->all()); // return empty Array
});
EDIT 2:
Googleing i found "createFromGlobals":
use Illuminate\HTTP\Request; // just added this to show which class i use here...
use Illuminate\Support\Facades\Route;
Route::post('/post_and_show', function () {
dd(Request::createFromGlobals()->get('post_var')); // returning expected value
});
This works for me, but i can't find this method in documentation. Sorry, i am new to laravel and even php, so this seems all completely crazy to me.. ;-)
EDIT 3:
If i use a simple Controller it works too...
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller
{
public function show(Request $request)
{
dd($request->post_var); // returns expected value...
}
}
Route:
Route::post('post_and_show', [FormController::class, 'show']);
So only in case i use the $request immediatly in the callback it does not work as expected. ( Either by design or bug??)

my delete query is not working in laravel

I am trying delete database data in Laravel. but this is not working my way.
my view page is
{{url('/deleteReview/'.$Review->id)}}
my web is
Route::post('/deleteReview/{id}','adminController#deleteReview');
my controller delete function is
public function deleteReview($id){
$deleteReview = Review::find($id);
$deleteReview->delete();
return redirect('/manageReview');
}
Are you trying to delete the review by opening the page /deleteReview/<id> in your browser? If so, this would be a GET request, so change the route to a get route:
Route::get('/deleteReview/{id}','adminController#deleteReview');
Please note as per the comments that a GET request should never change data server side. If data is changed using a GET request then there is a risk that spiders or browser prefetch will delete the data.
The correct way to do this in Laravel is using a POST request and use Form Method Spoofing to simulate a DELETE request. Your route entry would then look like this:
Route::delete('/deleteReview/{id}','adminController#deleteReview');
And your form would look like this:
<form action="/deleteReview/{{ $Review->id }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
At Controller you should first set Validation for ID that you have to Delete. Create your own customize request handler such as DeleteRequest.
Once you get ID at Controller then used this code
public function deleteReview(DeleteRequest $id){
DB::table('reviews')->where('id', $id)->delete();
return redirect('/manageReview');
}
I hope it will work.

Code Ingiter POST data empty

I'm just have a trouble when POST a multipart form with codeigniter. The form is contain text field and 8 field of file upload. the problem is, the file is uploaded successfully with no error, but the field of text is empty in my controller.
I tried to print_r the POST data and get an empty array. But sometimes when I'm using google chrome, It's not got any problem.
Any solution?
<form name="form" action="some action page.php" method="post" enctype="multipart/form-data">
<input type="text" name="inputfieldvalue" value="Input Field">
<input type="file" name="uploadedfile" id="uploadedfile">
<input type="submit" value="Submit" >
</form>
// suppose your form is like this as above then to check textfield value
<?php echo $_POST['inputfieldvalue']; ?>
// pass the name attribute whatever you have given to $_POST[your name field value]. like this
Complete example should be working.
your form target action to function my_form in My_controller controller. Lest try input with name WTF
<form name="form" action="my_controller/my_form" method="post" enctype="multipart/form-data">
<input type="text" name="WTF" value="Input Field">
<input type="submit" value="Submit" >
</form>
your cotroller action in My_controller.php file (CI Controller folder) should look le this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class My_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index(){
/* do something for page my_controller if you want */
} // end of index
function my_form() { // = my_controller/my_form
$here_is_it = $this->input->post('WTF'); // call value of input with name WTF
/* test it */
print_r($here_is_it);
}
Extra, to make sure everything it's okay. You can Enable auto load feature in Codeigniter. follow this tutorial
I Just check it for a while and the problem is not in the php or my html form code. But the problem is in my php.ini config. By increase upload max file size fix the problem. I just confused before because the form sometime work well and sometimes give an empty value.
Thanks in advance

Forms fields filtering issue in Magento 1.8.1.0

In account form fields or user registration page form fields for First name and Last name, when i place the following JS code :
<script>alert("Here")</script>
It is saved and is run on page load. This is very strange, because i checked the template files and values are escaped as below:
<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>
I have also confirmed if i am in correct template file by changing the label of field.
I have read the following questions and tried to used them but did not worked for me.
https://magento.stackexchange.com/questions/569/how-to-escape-output-data
https://magento.stackexchange.com/questions/8179/is-there-a-generic-way-i-can-apply-input-filtering-to-magento-form-processing
Regarding to the observer method, it works, but when i try to login to magento admin, i cant, but when iremove the observer, i can login again.
Check the two attached screenshots.
Kindly help me with this issue.
Thank you
I have created an observer. For login forms at admin and front end (and may be some other forms), the form fields for user name and password are in array format like below:
<input type="text" id="username" name="login[username]" value="" class="required-entry input-text">
<input type="password" id="login" name="login[password]" class="required-entry input-text" value="">
TO fix this issue, i have modified the code in observer as below:
public function sanitizeParams($observer)
{
if(!Mage::app()->getStore()->isAdmin())
{
$post = Mage::app()->getRequest()->getParams();
foreach($post as $pkey => $pvalue)
{
if(is_array($post[$pkey]))
{
foreach($post[$pkey] as $key => $value)
{
$post[$pkey][$key] = filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
else
{
$post[$pkey] = filter_var($pvalue, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
$_POST = $post;
}
}
And it fixed my problem, and is working fine now.
Thanks.

refresh page with joomla component

I have a simple form in my tmpl/default.php:
<form id='AddForm' action="<?php echo JRoute::_('index.php?option=com_mycomponent&task=addcam'); ?>" >
<p>
<label for="CamName">Name:
</label>
<input type="text" id="CamName" name="cam_name" />
</p>
<button type='submit' class='submit_cam' name='addcam' value='Add'>Add</button>
<button type='reset' class='cancel_changes' name='cancel_changes' value='Cancel'>Cancel</button>
</form>
In my controller.php file I'm trying to process the values:
function addcam()
{
$add_name=JRequest::getString('cam_name');
$model = &$this->getModel();
$model->AddWebcam($add_name); //send to model to add to DB
}
In my model I just return the result of the query. With this implementation I just get routed to an empty page. I'd like to have it refresh the current page. Typically you do this with action="" but in my case I need it to route to the function called addcam in the controller. Or is there a better way to do this?
A common technique in Joomla when directing to the task is to have that function do a full redirect to a view at the end. This prevents a page refresh from trying to resubmit the data and leads to a cleaner url for the client. To do this, try the following:
function addcam()
{
$add_name=JRequest::getString('cam_name');
$model = &$this->getModel();
$model->AddWebcam($add_name); //send to model to add to DB
JFactory::getApplication()->redirect(JRoute::_(index.php?option=com_mycomponent&view=whatever));
}
Obviously, update the JRoute bit to the url you actually need. You can also include a message if you would like (like "Saved!"): http://docs.joomla.org/JApplication::redirect/1.6

Resources