I have been struggling with something very simple here nevertheless the vagueness around Laravel's routing system that complicates its easy routing approach. I have gone through questions listed here but nothing seems to help me so here it goes.
I have formerly defined a route to my controller on an action named "create". This action is suppose to accept a post data from the form and persist it. This create method has one parameter which defaults to null for a project id if its add else we pass an id e.g domain/projects/add/22 to edit and domain/projects/add to create a new one.
Below is the skeleton of the function:
public function create( $id = null ){ ... }
I then defined a route for this which is:
Route::post( 'projects/add', 'ProjectsController#create' );
Inside my form I have {{ Form::open(array('url' => 'projects/add', 'method' => 'post')) }} .
I keep on getting errors related to routing, Http or method not found exceptions. I tried to follow every suggestion on the net but cannot for the life of me find my way.
Please help me point to the right direction, thanks.
try with below sample code
routs.php►
Route::post('projects/add/{id?}',array('as'=>'project_create','uses'=>'ProjectsController#create'));
ProjectsController.php►
class ProjectsController extends BaseController{
public function create( $id = null ){
...
}
projects.blade.php► (for used blade templating your views should have blade.php extention )
<html>....
<form action="{{ route('project_create') }}"method="post">
....
</form>
</html>
Thank you guys for all your responses. After being swamped with work I finally came back to my project and wanted to try out some of your suggestions but I failed.
I did find a tutorial that I tried to follow and basically was able to have my routes working.
Inside the routes I added (see below):
Route::get('/projects/list', [
'as' => 'post.list',
'uses' => 'ProjectsController#listProjects'
]);
Inside my controller I just created a function that is named listProjects(). As for getting my form displayed I followed the same pattern except point to newProject() method in my controller.
As much as I was not keen on this approach I ended up creating another function just for saving my POSTed form data after a new project form has been filled out and submitted. I still used the same url as projects/add except pointing it to a different function in may controller named saveProject().
About the view I just added the as part of the same save route and it worked. below is a link to the tutorial I followed and taking a look at the code.
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/
Related
I created the following route
Route::resource('posts','PostController');
Now I'm trying to create a custom route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
When the link posts/trashed is clicked, it goes to the PostController#trashed, but the controller shows blank page without any errors.
So changed the custom route to
`Route::get('post/trashed','PostController#trashed')->name('posts.trashed');
by simply changing the posts/trashed to post/trashed and the controller works fine.
Can someone explain what the issue is.
Figured it out. Solution was hidden deep inside the Laravel Documentation . https://laravel.com/docs/5.0/controllers#restful-resource-controllers
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:
Re-ordering will solve the issue.
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Route::resource('posts','PostController');
The resource route has some routes inside it own.
So this route:
Route::resource('posts','PostController');
contains this route :
Route::get('posts/{post_id}',PostController#show)
So it has conflict with your route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Solution 1:
bring your route upper than resource
I mean:
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed'); <-- first
Route::resource('posts','PostController');
Solution 2:
As you mentioned use another url (like post/trushed)
Solution 3 :
Add exception for resource.
Route::resource('posts', 'PostController')->except([
'show'
]);
I'm currently using laravel 5.4 and I have stumbled upon something I can't fix.
I'm currently trying to bind a route to a controller using the Laravel resource helper as such :
Route::resource('campaigns', 'CampaignsController');.
I correctly see my route being there when I do a PHP artisan:route list, I have all my CRUD endpoints tied to the appropriate controller function. Also, note that I'm currently doing that for all my route that need to be tied to a CRUD system ( what I'm working with is mostly form ) without any problem
With this being said, whenever I'm trying to edit a Campaign, I get an error : Class App\Http\Controllers\Ads\Campaigns does not exist
I do not know why it's trying to look for a Campaigns controller while I specify the CampaignsController controller. Everything is behaving correctly in campaigns route, except the edit one. Also, all my other routes have the same logic and never faced this problem.
Any idea why it is looking for the wrong Controller ?
Here's my namespace declaration and folder hierarchy, which is ok ( please note that the adsController has its routes declared the same way and is used the same way too )
here's my edit method
and here's the error
It's quite possible that you try to inject not existing class in your controller.
Take a look at controller constructor or edit route if you don't have something like this:
public function edit(Campaigns $campaigns)
{
}
and make sure you import Campaigns from valid namespace (probably it's not in App\Http\Controllers\Ads namespace.
If it doesn't help try to find in your app directory occurrences of Ads\Campaigns to see where it's used. Sometimes problem can be in completely different part of your application.
EDIT
Also make sure you didn't make any typo. In error you have Campaigns but your model is probably Campaign - is it possible that in one place you have extra s at the end?
Try with Route::resource('campaigns', 'Ads\CampaignsController'); in your web.php file
I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.
So sorry to bother y'all, but I was wondering if anyone could help me. I'm having a wee bit of a problem with my code in Laravel, being used to working from scratch rather than with frameworks. I made use of the Laravel Bootstrap Starter Site and I'm trying to add additional pages, but the routing isn't exactly co-operating. It's rather frustrating.
The Controller: app/controller/community/CommunityController.php
<?php
class CommunityController extends BaseController {
public function index() {
return View::make('community.index');
}
}
?>
The View
#extends('site.layouts.default')
{{-- Content --}}
#section('content')
#foreach ($posts as $post)
<div>
I'm just going to put this here...
</div>
#endforeach
{{ $posts->links() }}
#stop
And finally, last but not least, my routes.
Route::get('community', array(
'uses' => 'CommunityController#index',
'as' => 'community.index'
));
Now, I have this nagging feeling that I'm missing something rather small, but for the life of me I can't figure it out. If anyone would be so kind to explain what I'm doing wrong, I'd appreciate it. Especially since I can prevent this kind of problem happening in the future as well.
With friendly regards,
User who still hasn't picked out a good name
Edit: Sorry I forgot to mention this. I removed public, so I don't know if that influences anything. If it does, again, sorry for forgetting to mention this in the beginning.
you can try to bind the whole route to the controller, using
Route::controller('community', 'CommunityController');
then in your controller you have to prefix the controller methods with HTTP verbs.
Your index() method will be
public function getIndex() {
return View::make('community.index');
}
Just fire composer dump-autoload in root of your project folder from terminal / console.
It'll load your controller which is in subfolder.
I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.
How do I use my controller when I include my view?
I'm on Laravel 3.
Right now I have my controller :
public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}
My route (which obv only work on GET /current) :
Route::get('current', 'sale#current');
My master view
#include('sale.current')
Then my sale.current view calls $sales
#foreach($sales as $sale)
Thanks!
So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.
When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.
Here is the official documentation link : https://laravel.com/docs/master/views#view-composers
Here is the more detailed version of it :
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers
This is the standard way of achieving it without any patch work.
Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:
Route::get('test', 'TestController#test');
In TestController I define the action test as follows:
public function test()
{
return View::make('test.home')->with('data', array('hello', 'world', '!'));
}
According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:
// home.blade.php
<h1>Message</h1>
#include('test.test')
and
// test.blade.php
<?php print_r($data); ?>
When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.
I hope that can help you.