In a controller name tagController where I define a custom method
public function addTag($id)
{
$book = $id;
return view('tag.create', compact('book'));
}
In the route I define the custom route method
Route::get('tag/addTag/{$id}', 'tagController#addTag');
Route::resource('tag', 'tagController');
From my view I'm calling the controller method
<a class="btn btn-primary various" href="{{url('/tag/addTag', $tag->id)}}">Add Tag</a>
I'm getting the error every time
NotFoundHttpException in RouteCollection.php line 143:
This is routing problem but I don't understand to how to define a custom method in route and in resourceful controller. Please help to get rid of the error?
Thanks.
Remove the $ from the path
Route::get('tag/addTag/{id}', 'tagController#addTag');
Related
I have unique problem. I already create Route on web.php, on Controller, and Blade. and working on my localhost. but after publish to real server, I jus got error Action url\controller#function not define. this is my code.
Route::get('/timetableschedule', 'ManagementController#EmployeeTimeTable');
this is my web.php
public function EmployeeTimeTable(Request $request){
$session = $request->session()->get('user.id');
$companysession = $request->session()->get('user.location');
$locationdata = DB::table('company')
->join('companyrole','companyrole.company_id','=','company.Company_id')
->join('users','users.id','=','companyrole.user_id')
->where('users.id',$session)
->get();
$userlist = db::table('users')
->join('companyrole','users.id','companyrole.user_id')
->where('companyrole.company_id',$companysession)
->whereNotNull('users.NPK')
->select('users.id','users.name','users.NPK')
->orderby('users.name')
->get();
$timesetting = db::table('time_leaving')
->where('company_id',$companysession)
->get();
$leaveschedule = db::table('employee_leaving')
->join('users','employee_leaving.user_id','users.id')
->where('employee_leaving.company_id',$companysession)
->select('employee_leaving.leaving_date','users.name')
->get();
return view('Management.employeetimetable',['location'=>$locationdata,'UserList'=>$userlist,'ListTimeSet'=>$timesetting,'LeavingSchedule'=>$leaveschedule]);
}
this is my controller
<li>
<a href="{{action('ManagementController#EmployeeTimeTable')}}" class="dropdown-item">
<p>Employee Timetable</p>
</a>
</li>
and this is code on blade to call controller#function
this step same as another controller, web, and blade.php. but, just for this rout get me a trouble. thank you
You have a problem with your route cache, simply do this:
php artisan cache:clear
php artisan route:cache
if you want to do same on the server you can define a route in web.php for that like this:
Route::get('/clear/route', 'ConfigController#clearRoute');
and make ConfigController.php like this
class ConfigController extends Controller
{
public function clearRoute()
{
\Artisan::call('route:clear');
}
}
and then go to that route on server example http://your-domain/clear/route
If your controller is on a subfolder, and not in app\Http\Controllers you will need to provide the fully qualified class name of the controller to the action helper :
action('App\Http\Controllers\Admin\ManagementController#EmployeeTimeTable')
by default action helper will look for : App\Http\Controllers\ManagementController
note that this syntax will not work as you might expect :
action('Admin\ManagementController#EmployeeTimeTable')
because laravel will not add App\Http\Controllers if there is a \ in the action name parameter
This error is comeup while i working on laravel 8
Route [product.details] not defined. (View: C:\xampp\htdocs\ecommerce\resources\views\livewire\shop-component.blade.php)
here is the route i made Route Page web.php
Route::get('/product/{slug}',DetailsComponent::class)->name('product.details');
this is line of view where i want that route View Page shop-component.balde.php
<a href="{{route('product.details',['slug'=>$product->slug]) }}" title="{{$product->name}}">
and this one is the Details Component code
namespace App\Http\Livewire;
use Livewire\Component;
class DetailsComponent extends Component
{
public $slug;
Public function mount($slug){
$this->slug = $slug;
}
public function render()
{
$product = Product::where('slug', $this->slug)->first();
return view('livewire.details-component',['product'=>$product])->layout('layouts.base');
}
}
I think this php code doesnot get route of product.details at the same time i create middleware file and define a different route in it this route is working in this page but not Product.details.
Kindly help me i am stuck from 1 week here
You can use
php artisan route:list
to show a list of registered routes - make sure it shows up there. If it doesn't, use
php artisan route:clear
to clear Laravel's route cache
It does not work because of your route. It's not write in the correct way. You have first to make your DetailsComponent::class is bracket then you have to give the name of the method you want to use in this class.
Route::get('/product/{slug}', [DetailsComponent::class, 'method name'])->name('product.details');
I will get MethodNotAllowedHttpException when submitting a form in laravel
Html file
<form method="POST" action="/cards/{{$card->id}}/notes">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<textarea name="body" class="form-control"></textarea>
<button type="submit">Add Note</button>
</form>
routes.php
Route::post('cards/{card}/notes','NotesController#store');
NotesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store()
{
return request()->all();
}
}
Make sure you don't have a route, say a Route::post with a parameter that lies in front of the route you are trying to hit.
For example:
Route::post('{something}', 'SomethingController#index');
Route::post('cards/{card}/notes', 'NotesController#store');
In this case, no matter what you try to send to the cards route, it will always hit the something route because {something} is intercepting cards as a valid parameter and triggers the SomethingController.
Put the something route below the cards route and it should work.
MethodNotAllowedHttpException is thrown when no matching route (method and URI) was found, but a route with a matching URI but not matching method was found.
In your case, I guess the issue is because URI parameters differ between the route and the controller.
Here are two alternatives you can try:
Remove the parameter from your route:
Route::post('cards/notes','NotesController#store');
Add the parameter to your controller:
public function store($card)
{
return request()->all();
}
I have tried to solve this error in lumen and it took me quite a lot of time to figure out the problem.
The problem is with laravel itself.
Sometimes if you have another route like GET device/{variable}, laravel stops in this first route...
So what you need to do is change the route POST device to POST device/add
This link helped me a lot
Its Laravel 5.
When the route.php contains this:
Route::get('/foo', function () {
return 'Hello World';
});
then the page shows with the text "Hello World".
However, as soon as I add this new line in route.php:
Route::get('/foo2', 'IndexController');
then the page show this error:
UnexpectedValueException in Route.php line 567: Invalid route action: [App\Http\Controllers\IndexController]
I previously created a controller with artisan which now looks like this:
class IndexController extends Controller
{
public function index()
{
echo 'test';
}
}
what am I doing wrong?
You have to specify wich method will be executed:
Route::get('/foo2', 'IndexController#index');
If you are using get method of Route. Normally first argument provided should be the url and second argument should be the method (there are other ways argument could be passed)
Route::get('/foo2', 'IndexController#index');
If you want to resourceful route . Normally first argument should be the resource name and the second argument should be RESTful controller name. (there are other ways argument could be passed).Example: photo is the resource name and PhotoController is the controller name.
Route::resource('photo', 'PhotoController');
in your case it should work this way
Route::resource('/foo2', 'IndexController');
or
Route::get('/foo2', 'IndexController#index');
so when you visit
yoursite.com/foo2
you will be displayed with IndexController index method
See reference more to learn laravel's restful resource controller
reference: https://laravel.com/docs/5.1/controllers#restful-resource-controllers
You need to specify the function inside the controller not just the controller:
Route::get('/foo2', 'IndexController#index');
You have to reference Controller#method as:
Route::get('/myroute', ['uses' => 'MyController#methodName']);
I'm new in laravel and I got an error and don't really know how to fix it.
I got an error "Controller method not found" when i'm asking for this route : /projet/6/note
My routes.php
Route::controller('projet.note', 'NoteController');
Route::resource('/eleves', 'StudentController');
Route::controller('/auth', 'AuthController');
Route::resource('/user', 'UserController');
Route::resource('/projet', 'ProjectController');
Route::post('/eleves/search', 'StudentController#postSearch');
Route::resource('/classe', 'ClasseController');
Route::controller('/', 'HomeController');
I tried to type php artisan routes to see if the routes was working, and she's not.
I tried then to change controller into resource in the line about NoteController, the routes was there but when i go on the link, same error.
Then i guess i can't do 'projet/note' without that my NoteController is a resource?
It's a problem because i need to nest NoteController to ProjetController.
My only action in NoteController
public function getIndex($id)
{
return View::make('note.noter')
->with('project', Project::find($id))
;
}
Thanks
I hope I understood your question right. This is the best solution I could come up with:
Route::any('projet/{id}/note/{action?}', function($id, $action = 'index'){
$controller = App::make('NoteController');
$action = strtolower($_SERVER['REQUEST_METHOD']).studly_case($action);
if(method_exists($controller, $action)){
return $controller->callAction($action, [$id]);
}
});
This basically does some similar things as Route::controller. First we create a controller instance, then build the action name out of the request method and the second parameter in the route and in the end, call the action (if it exists).