How to remove the $_GET variable after processing it in Laravel? - php

So I got a little problem. I'm currently learning how to handle laravel. I was okay until now. I`ve tried to create a small page. 4 Articles, 1 Basket, when I click a button, I just hand over the id and the name of the article via $_GET and put it into the basket. This is the controller i have for now:
class FileviewController extends BaseController {
protected $layout = "layouts.master";
public function getIndex() {
if(isset($_GET["article"])) {
Session::push("basket.items", filter_var($_GET["article"]));
$items = Session::all();
}
$articles = Article::with('manufacturer')->get();
}
$articles = Article::with('manufacturer')->get();
$this->layout->content = View::make("article.index", array("items" => $items, "articles" => $articles));
}
At first ive tried to use header() and redirect him on the current page without the parameeter ... didnt work for some reason:
if(isset($_GET["article_id"])) {
Session::push("basket.item_id", filter_var($_GET["article_id"]));
Session::push("basket.item_name", filter_var($_GET["article_name"]));
$items = Session::all();
$articles = Article::with('manufacturer')->get();
header('Location: http://10.36.155.40/laravel/public/article');
}
Didn't work out. Is there any other way to remove the variables from the URL after they have been processed? Because even if the user reloads, the variable is still there and will be added to the basket again. For obvious reasons, i have to avoid that.
I guess i just cant see the forest because of all the trees. Is there even an easier way to do that?
Any help is appreciated. :)

You should just redirect them using Laravels Redirect facade. Gone are the days of setting Location headers! Full docs located here. Here are a few examples:
return Redirect::to('/');
return Redirect::route('my.named.route');
return Redirect::back()->with('errorMessage', 'Get out of here!');

Related

Passing parameters in CodeIgniter

I have been banging my head against this problem for about an hour now. I have looked high and low but nothing is working for me. This should be simple and I am sure it is.
I am trying to pass some parameters in CodeIgniter to a URL and nothing seems to be working. Here is my controller:
class Form_controller extends CI_Controller {
public function change($key = NULL) {
if (is_null($key)) {
redirect("reset");
} else {
echo "Hello world!";
}
}
}
Here is my route:
$route['change/(:any)'] = "form_controller/change/$1";
Every time I visit /index.php/change/hello I get the string "Hello world!" but when I visit /index.php/change I got a 404 not found.
What I am trying to do is pass a parameter to my controller for the purposes of checking the DB for a specific key and then acting upon it. If the key does not exist in the DB then I need to redirect them somewhere else.
Any thoughts on this?
Never mind, I figured it out. I ended up making two different routes to handle them, like so:
$route['change'] = "form_controller/change";
$route['change/(:any)'] = "form_controller/change/$1";
And the function in the controller looks like this now:
public function change($key = NULL) {
if (is_null($key)) {
redirect("reset");
} else if ($this->form_model->checkKey($key)) {
$this->load->view("templates/gateway_header");
$this->load->view("forms/change");
$this->load->view("templates/gateway_footer");
} else {
redirect("reset");
}
}
If anyone has a better solution, I am all ears. This worked for me though.
This might help you out
public function change() {
$key = $this->uri->segment(3);
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
This allows you to grab the segment easily using the CI url helper
index.php/change/(3RD Segment) this part will go into that variable $key.
This may not be what you are looking for but it is very useful if you are trying to pass two variables or more because you can just grab the segment from the url and store it into the variable

Where to put code in Model View or Controller to manage User priviliges and url manipulation protection in cakephp?

Apologies if this has been asked before, I've done a search and haven't found anything specific. This is has been helpful http://bakery.cakephp.org/articles/Auzigog/2008/12/29/where-should-my-code-go
I'm trying to fix some code I inherited and I found you can change anyone's password, just change the URL:
/site/user/changepassword/(insert id)
I then placed in the user controller, pardon my pseudocode:
if(session.user_id == id_from_link)
view changepasswordform(id_from_link)
else
warn_and_redirect();
I think that was the right thing to do and in the right place?
Now in the Views I find code like this:
if(user_type is admin)
echo admin options
if(user_type is user)
echo user options
Now shouldn't that ideally be the View just having:
echo options
and then the Controller has:
switch(user_type)
case: admin
options = admin stuff
case: user
options = user stuff
and so on? or should this be in the User Model?
Just remove the id parameter from the url... and at the top of the controller action add this:
function changepassword(){
$id = $this->Auth->user('id');
....
}
Now the password will only be changed on the current user that is logged in. Be sure to do your normal checks of making sure $id is not null.
View should effectively be print statements:
<title><?=$this->data['title']?></title>
...
<h1><?=$this->data['main_menu']?></h1>
Controller should prep the view/handle the request:
if(loggedInUser) {
$this->data['title'] = model->getTitle(userID);
$this->redirect(/somepage);
}
else {
$this->redirect(/loginpage);
}
Model should have:
function getTitle($userID) {
this->doStuff($userID);
$title = this->talkToDB($userID);
return $title;
}

CakePHP Redirecting back to the same page

I have a Model for Groups and another model for Notes (Notes and Posts are same things).
NotesController:
public function groupnotes()
{
if (!empty($this->data))
{
$data = $this->data;
$data['Note']['user_id'] = $this->Auth->user('id');
if ($this->Note->save($data))
{
PROBLEM HERE
}
}
if(empty($this->data['Note']['notes']))
{
PROBLEM HERE
}
GroupsController: (ViewCourse is used to view each group )
public function viewcourse($id=NULL)
{
$this->set('viewcourse', $this->Group->read(NULL,$id));
$this->set('course', $this->Group->find('all', array('conditions'=>array('Group.id'=>$id))));
}
Now when i create a post in a group it redirects me to "groupnotes" action and i want it to redirect me to viewcourse/id ... I am a bit confused how can i redirect the page to viewcourse/id ...
I tried doing it by adding this to groupnotes action
$this->redirect(array('controller'=>'groups', 'action' => 'viewcourse'));
but here i do not have the id.
What do you suggest?
This question might help you: What is the equivalent to getLastInsertId() in Cakephp?
$this->redirect(array(
'controller'=>'groups',
'action' => 'viewcourse/'.$this->Group->getLastInsertId())
);
EDIT:
I have only suggested that you go to the last inserted id of a group as a suggestion. Your question is a bit vague when you say "but here i do not have the id."
1. are you looking to go to any valid course id?
2. last entered course?
3. first entered course id?
Alternatively you could set a default course in your controller like so...
public function viewcourse($id=NULL)
{
if(!$id){
$id = $this->Group->find('first');
$id = $id['Group']['id'];
}
$this->set('viewcourse', $this->Group->read(NULL,$id));
$this->set('course', $this->Group->find('all', array('conditions'=>array('Group.id'=>$id))));
}
NOTE: Just a tip,
$this->set('course', $this->Group->find('all', array('conditions'=>array('Group.id'=>$id))));
Can be substituted with
$this->set('course', $this->Group->findById($id));
To make your code a bit leaner

Codeigniter tutorial delete Post doesn't work

After completing the tutorial from the codeigniter user guide I ran into a problem I was forcing for the last two hours. I am trying to add functionality to delete a post, selected by ID, I am new to PHP and couldn't find any solution for my problem:
The Controller
public function delete($id){
$id = $this->uri->segment(3);
$data['title'] = 'Delete an item';
if($this->news_model->delete_news($id)){
$this->load->view('templates/header', $data);
$this->load->view('news/success');
$this->load->view('templates/footer');
}else{
}
}
The Model
public function delete_news($id){
$this->db->where('id',$id)->delete('news');
return $this->db->affected_rows();
}
The Routing
$route['news/delete/(:num)'] = 'news/delete/$1';
I'm calling the function out of the index-page where all posts are shown with an anchor:
<p>Delete article</p>
and it calls the correct URL (http://localhost/webapp/index.php/news/delete/2) which should correctly execute and delete the post with the ID 2 from my news table.
I really can't understand where the mistake ism but by executing this, I get a 404.
What am I doing wrong?
In your function delete I don't see that you loaded the news_model. That could be the issue if it isn't auto-loading. Perhaps, start by verifying that the controller is talking to the model by inserting:
echo 'Hello Model';
in the delete_news function of your news_model.
EDIT:
Instead of
if($this->news_model->delete_news($id)){
//conditions
}
And
Have your model send a T/F based on it's execution. This will tell us if the error is in the SQL. By returning TRUE no matter what, we'll see if that model function even runs:
return TRUE;
Try to add the step (for error checking)
$del = $this->news_model->delete_news($id);
echo 'del';
if($del == TRUE){
//conditions
}
With the 404 - I'm also suspicious it's a routing issue. I'll take a look at that as well.

How to convert an existing PHP library file so it can be used in a CakePHP framework?

I have this library in PHP non-Cake format, the usual PHP scripting which currently works like a charm. I need to use this in a Cake framework. The library file is as follow: (example extracted)
<?php
// REST API functions
function sendAction($itemurl, $itemimageurl, $sessionid, $userid, $rating=""){
global $someapiwebsiteURL, $apiKey, $tenantId;
$somewebsiteAPI = $someapiwebsiteURL.$action."?apikey=".$apiKey.
.....
................
}
//Codes extract
?>
I've come across a few ways of doing it. Currently confused, how am I going to place this library file into my Cake framework?
App::import()
Datasource
The functions in the library file above (I supposed it'd be used in one of my Controllers to render the data outputting through the view).
Currently working in a non-Cake framework structure, the view page is such as: (example extracted)
<?php
// my view page
$viewResponse = sendAction($itemdescription ,$itemurl , $itemimageurl,$sessionid,$userid);
//sample code only
?>
Both the files are working fine. The logic of putting it in a CakePHP framework is the problem here. Anyone may suggest "the" way of doing this without over-strenuously working on a data source? If we have to use a data source in App/models/datasources/, how exactly is the structure of it? Like, e.g., in datasource file, do we include the library functions? or is it some generic ReST datasource file which can be found here: CakePHP ReST datasource . I've gone through the cookbook chapter on datasource and understand we have to define the datasource in our database.php, but if someone is certain about their way of accomplishing it either using datasource or app::import() method, please share with more details?
UPDATE:
Hi Lionel!, thanks for filling up. Well, actually users will click on view action: function view (){} in my foods_controller. I'm appending some scripts here to include my view function in my foods_controller so maybe it may help you to help out easier. Thanks..
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
$this->set('food', $this->Food->read(null, $id));
}
The view action triggers the send_action function, (each time a user clicks on view page on foods controller). So each time, a user clicks on view action, his (dynamic variables): userid, sessionid, that page's itemid, url, itemdescription; (timerange value is a static string value "ALL"), and if any (etc.), so far only these values are available: Will be used as the "parameters" in the Send Action function. What you wrote is close to what the codes can do. You're right. Except we should include the Send Action function inside the view() in foods controller?
If we look at dynamically filling in the variables mentioned in the point above, could you modify your second code (the code from your product_controller, e.g.) so it also works to receive the variables dynamically? (as you asked in the last update: how to get the parameters..)
Just to make it clear.
A user views the page. The send action collects data and send to the API. (as we've already done by calling the function in the library the (ACME.php). *just waiting for your update if possible, thanks.
In the function view() of the foods controller: there's also an additional calling. The (2)second calling which is this:
$recommendResponse = getRecommendations("otherusersviewed", $itemId, $userId);
The second calling calls the ACME.php library file in which there consists the (2)second function that retrieves data, here it is: (it's in working order, but just needs to be changed into a public static function like you did for the (1)first function. Could you help to modify this code too, please?:
function getRecommendations($recommendationType, $itemId, $userId){
// sample code similar to the first one.
}
That's all to it. It seems quite simple in the normal PHP format, and it works easily, but getting it on an MVC framweork is a bit challenging for some, a lot for me. Thanks for helping out, Lionel. :-)
P.S. Hi Lionel, I notice something missing in the library after changes? Look originally we have this:
$somewebsiteAPI = $someapiwebsiteURL.$action."?apikey=".$apiKey.
Look, the variables for $SomeWebsiteAPI and $SomeApiWebsiteURL are different. Did I miss out something? or you have modified so it is more efficient ? I see that the variable named $SomeWebsiteAPI is modified to become variable called $link ? and variable $SomeApiWebsiteURL is changed to the named variable, $url, am I right ? .. thanks.
Thanks, best regards. John Maxim
To me, if I have this piece of code, I would first wrap it into a static (or normal) class, and named it ACME, then I will move the acme.php into /apps/libs/acme.php. Then in the controller, I will use App::import('Lib', 'acme'). This action do nothing but just requiring the file, so you can just use it instantly by calling ACME::sendAction(...).
And regarding the global thing, you might just need to declare a static (or normal) class, then define the shared variables as part of the class properties, so you can share them among all the functions in the class.
For example, this is the /app/libs/acme.php
class ACME {
private static $someapiwebsiteURL = "http://thewebsite/api/1.0/";
private static $apiKey = "0010KIUMLA0PLQA665JJ";
private static $tenantId = "THE_TENANT_NAME";
/**
* Simple builder to build links from array of $params
*
* #param string $url The api url
* #param array $params The given parameters
* #return string built url
*/
private static function BuildLink($url="", $params=array()) {
$link = $url;
foreach($params as $k=>$v) {
$link .= "&$k=$v";
}
//Replace the first & to ?
$link = preg_replace("/&/", "?", $link, 1);
//Not sure if we need URL encode here, please uncomment this
//if the API could not work.
//$link = urlencode($link);
return $link;
}
public static function SendAction($action, $itemId, $itemdescription, $itemurl, $itemimageurl, $sessionid, $userid, $rating="") {
$somewebsiteAPI = self::BuildLink(self::$someapiwebsiteURL.$action, array(
"apikey"=>self::$apiKey,
"sessionid"=>$sessionid,
"userid"=>$userid,
"tenantid"=>self::$tenantId,
"itemid"=>$itemId,
"itemdescription"=>$itemdescription,
"itemurl"=>$itemurl,
"itemimageurl"=>$itemimageurl,
/**
* Assuming your API smart enough to only use this value when
* the action is "rate"
*/
"ratingvalue"=>$rating
));
$xml = simplexml_load_file($somewebsiteAPI);
return $xml;
}
public static function GetRecommendations($recommendationType, $itemId, $userId) {
$somewebsiteAPI = self::BuildLink(self::$someapiwebsiteURL.$recommendationType, array(
'apikey'=>self::$apiKey,
'tenantid'=>self::$tenantId,
'itemid'=>$itemId,
'userid'=>$userId
));
$xml = simplexml_load_file($somewebsiteAPI);
return $xml;
}
}
And in your controller
App::import('Lib', 'acme');
class FoodController extends AppController {
//Food is plural already I assume? You can just use
//food, should be ok I think, else it will be weird
//to use /foods/view/?
var $name = "Food";
var $uses = array("Item", "Food");
function view($id="") {
//We accepts only valid $id and $id > 0.
//Take notes that this $id will be a string, not int.
if (ctype_digit($id) && $id > 0) {
//I don't know how you would gather the information, but I assume you
//have a database with the information ready.
//I assumed you have an `items` table
$item = $this->Item->findById($id);
$sessionid = "00988PPLO899223NHQQFA069F5434DB7EC2E34"; //$this->Session->...?
$timeRange = "ALL";
$userid = "24EH1725550099LLAOP3"; //$this->Auth->user('id')?
if (!empty($item)) {
$desc = $item['Item']['description'];
$url = "/foods/view/".$id;
$img = $item['Item']['img'];
$viewResponse = ACME::SendAction("view", $id, $desc ,$url, $img, $sessionid, $userid);
$this->set('food', $this->Food->read(null, $id));
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}else{
$this->Session->setFlash(__('Invalid food', true));
$this->redirect(array('action' => 'index'));
}
}
}
Edit
The code has been filled up, and of course, without any warranty :). I personally don't really like to have long arguments in a function (like SendAction, error prune), rather use shorter one like the $params in ACME::BuildLink. But just to respect your code, I didn't modify much on the SendAction method.
Then I'm not too sure how you would make use of this code, so I assumed you have a ProductsController, and somehow the user trigger url like /products/send_action/. If you can provide more information, then we would be able to help out.
Edit Again
I have modified the ACME class, as well as the controller. Yea I do miss out some variables, but I had added them back to the updated code.
Not too sure if it would work (perhaps typo), you can just modify the code if it doesn't work for you.
And for personal conventions, I usually capitalize methods which are static, like ACME:GetRecommendations or ACME::SendAction.
Oh yea, I better stick back to the variables you used. Sorry for modifying them, just I don't like long names :)
And btw, the RoadRunner's ACME Corporation? Lol!
Cheers
Lionel

Categories