I wants to make a list page and add page
for list page my route function is
Route::get('admin/auctionlist','AdminController#showAuctionList');
and controller is
public function showAuctionList(){
$auctions = DB::table('auctionitems')
->leftjoin('campaigns','campaigns.id','=','auctionitems.campId')
->select('auctionitems.*','campaigns.title')
->get();
return View::make('admin/auctionlist')->with('auction',$auctions);
}
it works fine and my url is
http://localhost/vishal/site/public/admin/auctionlist
And for my add page route is
Route::post('addAuction',function(){
$obj = new AdminController() ;
return $obj->addAuction();
});
controller is
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return $this->showAuctionList();
}
It redirects to list page but url showing as
http://localhost/vishal/site/public/addAuction
Aucually i want to the url as
http://localhost/vishal/site/public/admin/auctionlist
how can i get it.?
Finally i solved the issue.
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return Redirect::to('admin/auctionlist');
}
its in laravel 4.
in laravel 5
it should be
return $this->redirect()->route('some-route-name');
Just use return redirect. And why messing your routing with functions? Just rule it to use the function in your controller:
routing
Route::post('admin/auctionlist','AdminController#addAuction');
controller
public function addAuction(){
AuctionModel::addAuctions(Input::except(array('_token')));
return redirect('admin/auctionlist');
}
Related
I'm a newbie to the Laravel framework. What I want to do is to redirect the users to a wait page firstly, then, they'll be redirected to their homepage after the wait screen.
I wrote some code for this but unfortunately it does not work. I searched about it but couldn't find a solution.
Here is my code:
function order(Request $req){
$data = [['user_id'=>Auth::id(),'order_note'=>$req->order_note, 'sugar'=>$req->order_sugar, 'amount'=> $req->amount, 'place_id'=>$req->order_place, 'beverage_id'=>$req->order_beverage]];
Orders::insert($data);
$this->redirectToWait(); // this is another function in the same controller.
sleep(10);
return redirect()->route('home');
}
function redirectToWait(){
return redirect()->route('wait');
}
This code only returns to the home route, it skips the redirectToWait() function.
Thank you for your help :)
You can't do this only using PHP & Laravel.
You have to use JavaScript to load first page then second page.
Controller:
function order(Request $req){
$data = [['user_id'=>Auth::id(),'order_note'=>$req->order_note, 'sugar'=>$req->order_sugar, 'amount'=> $req->amount, 'place_id'=>$req->order_place, 'beverage_id'=>$req->order_beverage]];
Orders::insert($data);
return view('wait-step-view-here');
}
wait-step-view-here.blade.php
...[SOME_HTML_HERE]
<script>
setTimeout(function(){
window.location.replace("{{ route('home') }}");
}, 10000);
</script>
...[SOME_HTML_HERE]
I've been using Laravel-5.8.35. I was invoking a GET request through a form. On my route, I redirected the form action to the same controller where the form was submitted but redirected through a different route, as,
$router->get('/merchant/sd-refund', 'Reports\ReportController#refundSecurityDeposit');
And, in my refundSecurityDeposit method, I called my SohojSdRefundService service,
public function refundSecurityDeposit(Request $request)
{
// $userId, $reason, $sdAmount was fetched from the $request instance
$this->execRefundRequest($userId, $reason, $sdAmount);
}
public function execRefundRequest($userId, $reason, $sdAmount)
{
// here the API service request data was handled,
// and stored in $data instance
return SohojSdRefundService::callSdRefundApi($data);
}
While my SohojSdRefundService service was done handling, I wanted to redirect the route to another route, as,
class SohojSdRefundService
{
public function __construct()
{
}
public static function callSdRefundApi($requestData)
{
// call to other methods inside the class to handle the API response,
// and then return to the same route which isn't working
return redirect('/admin/merchant/list');
}
}
Respectively, instead of redirecting to that route, the page happens to be still on the /merchant/sd-refund?... where the form was submitted initially. I redirected another service like this, which is working fine though. Could anyone suggest what I could be implementing wrong here? TIA.
You need to return a result in refundSecurityDeposit fucntion
public function refundSecurityDeposit(Request $request)
{
return $this->execRefundRequest($userId, $reason, $sdAmount);
}
I have started working with Codeigniter. So, I have two controller Tasks and Project. In my Task Controller I have a function to edit an existing task and after the task is edited I want to redirect to the Projects controller's display method which takes a parameter as the project_id associated with the task.
Here is my task controller:
public function edit($task_id,$task_project_id){
$this->form_validation->set_rules('task_name', 'Task Name','trim|required');
$this->form_validation->set_rules('task_description', 'Task Description','trim|required');
if ($this->form_validation->run() == FALSE){
$result=$this->task_model->get_task($task_id);
$data['task_name']=$result->task_name;
$data['due_on']=$result->due_on;
$data['task_description']=$result->task_description;
$data['id']=$task_id;
$data['task_project_id']=$task_project_id;
$data['main_view']="tasks/edit_task";
$this->load->view('layout/user', $data);
} else {
$data['task_project_id']=$task_project_id;
$data['task_name']=$this->input->post('task_name');
$data['due_on']=$this->input->post('due_on');
$data['task_description']=$this->input->post('task_description');
$data['id']=$task_id;
if ($this->task_model->edit($data)){
$this->session->set_flashdata('task_edited' ,'Task Edited Succesfully');
redirect(projects/display/$task_project_id); THIS SEEMS TO BE THE PROBLEM .
}else{
$this->session->set_flashdata('task_edited_failure' ,'Task Not Edited Succesfully');
redirect(projects/edit);
}
}
}
Here is my Projects Controller with its index and display function :
public function index()
{
$data['projects']=$this->projects_model->get_projects();
$data['main_view']="projects/home_view";
$this->load->view('layout/user.php',$data);
}
public function display($project_id){
$data['task']=$this->task_model->get_tasks($project_id);
$data['project']=$this->projects_model->get_project($project_id);
$data['main_view']="projects/display";
$this->load->view('layout/user.php',$data);
}
So, in my task controller I am trying to redirect to the projects controller's display method using redirect(projects/display/$task_project_id) as the Projects Controller's display function is display($project_id). But this is redirecting to me the Project controller's index function. What is wrong with the redirect ? I thought the URL structure in CI was controller/function/parameter1/parameter2.
You made mistake in writing redirect function. It should be like as below.
redirect('projects/display/'.$task_project_id);
redirect('projects/edit');
redirect($uri = '', $method = 'auto', $code = NULL)
redirect() function in codeigniter uses the url you define in your root. You can't directly jump to controllers.
I want to pass a variable to my ajax rendering function.Something like this
$this->render('tempcomment/'.$id, 'ajax');
and my tempcomment action is
public function tempcomment($id) {
$commentdata = $this->Post->Comment->findByPostid($pid);
$this->set('commentdata', $commentdata);
}
but rendering is not working.
See below to start with:
public function tempcomment($id) {
// ^ ------ ...
$commentdata = $this->Post->Comment->findByPostid($pid);
// ^--- this is different!
$this->set('commentdata', $commentdata);
}
render is used just to decide what view to use in your controller action to render the action. It does not call the action itself
so if you have a code like this
public function test() {
$this->set('foo', 12345);
$this->render('tempcomment', 'ajax');
}
you are just saying that you want to use the tempcomment view to render the test action but you are not telling cake to actually execute the tempcomment action.
so if you want to call the action you have to do by yourself
$this->tempcomment($id);
$this->render('tempcomment/'.$id, 'ajax');
let me tell you that I don't see any reason to do this and I think that probably there are other ways to do what you are trying to achieve. But I assume you know what you are doing
Try this, its another work around of what you want to achieve:
public functionc ajax_tempcomment($id){ //just access this action
$this->redirect('tempcomment/'.$id);
$this->layout = "ajax";
}
public function tempcomment($id) {
$commentdata = $this->Post->Comment->findByPostid($pid);
$this->set('commentdata', $commentdata);
}
I have a filter that runs on all my controllers..
in preFilter I have:
protected function preFilter($filterChain){
Yii::app()->params->controller = Yii::app()->controller->id;
Yii::app()->params->action = Yii::app()->controller->action->id;
return true;
}
in the postFilter i have:
protected function postFilter($filterChain){
$this->controllerName = Yii::app()->params->controller ;
$this->actionName = Yii::app()->params->action;
$this->CheckTrigger();
return true;
}
The function CheckTrigger() refers to Yii::app()->params->controller and Yii::app()->params->controller.
So here is the issue.. If i have a redirect in my action this brakes. If i comment out my redirect it starts working again. It seems that when the redirect is called the Pre and Post Filter actions are also called but the Post filter is never called before the redirect is initiated.. Is there a way to ensure that the post filter is called before a redirect? Am i missing something here?
many thanks..
Try this redirect code in your controller action:
$this->redirect('url here', false);