Laravel two similar routes, second route not found - laravel

Laravel 5.8, I have two routes:
Route::get('/exam-info/{id}', 'ExamController#mainExamInfo')->name('main_exam_info');
Route::get('/exam/{id}', 'ExamController#startMainExam')->name('start_main_exam');
When I go to the second route it gives me "page not found" error! Why?
Update:
In ExamController my startMainExam function is:
public function startMainExam($exam_id){
dd("sss");
// other stuff
}
But when I change the second route to:
Route::get('/exaxxxx/{id}', 'ExamController#startMainExam')->name('start_main_exam');
It works!!

Do you defined resource route on ExamContorller. Something similar like below on your route.php
Route::resource('exam', 'ExamController');
If it present, please change the second route name to something else like /exam-id/{id}

Related

"app\Http\Controllers\" does not exist") laravel 8

I just want to echo Hello world through Route
Route::get('/user',[addnewcontroller::class, 'getData']);
this is my web.php Route
and this is controller
class addnewcontroller extends Controller
{
function getData()
{
echo "My name is hassan";
}
}
whenever i try to execute it say controller doesnt exist, kindly let me know why im facing this error, ive tried many things like clearing caches, code etc
you need to define path of controller in web.php
like this:
use App\Http\Controllers\addnewcontroller;
You must use App\Http\Controllers\addnewcontroller at the top of routes

Laravel if route condition not met then ignore

I have the following 2 routes:
Route::get('/{category}/{slug?}', [CategoryController::class, 'index'])->name('category.index')->where('slug', 'trending|subtitled|feature');
This route
Route::get('/cookies-policy', [PageController::class, 'cookiesPolicy'])->name('cookiesPolicy');
When i try to access the second route, the first route controller is accessed instead.
How do i add a condition to the first route, that if the first segment isn't trending,subtitled or feature then ignore? I thought i accomplished this with: ->where('slug', 'trending|subtitled|feature')
You would need to adjust your where to be assigning the pattern to the category route parameter not the slug parameter:
->where('category', ...);

search routing in laravel 8, error " page not found "

What should be the route for the search page in laravel 8? My routes are...
List of routes :
Redirect that I use in the search method in the controller:
enter image description here
please can someone help me I'm blocked?
with get method, you've called two times /search URL.
The first is for personnel and the second is for material. While running route:list personnel route override by material. Hence, It show only last route.
You should have one URL for any route.
Route('/search/{type}',MaterialController#search)->name('type.search');
Now you can use it in a dynamic way.
{{ url('search/material')}}
{{ url('search/personnel')}}
{{ route('type.search','material')}}
{{ route('type.search','personnel')}}
In the controller, you can make it as dynamic as below.
public function invoicestorecentiga($type) {
if($type == 'material'){
}
if($type == 'personnel'){
}
}
You have two same search routes in your web.php file. Even if you named them differently, your second search route is overwriting the first one, and the first route basically doesn't exist anymore. My suggestion would be to rename one of those routes to something else, example:
Route::get('materials/search', ....)
Route::get('personnel/search', ....)

Laravel Api.php route group naming conventions

I have a group definition inside api.php.
I wonder why the first controller works fine
but the second would return Target class [UserExpertController] does not exist.
I like the second syntax more as im used to it from writing web.php routes.
any idea?!
Route::name('experts.')->prefix('experts')->group(function () {
// returns all experts
Route::get('/',[UserExpertController::class, 'index'])->name('index');
//or
Route::get('/','UserExpertController#index')->name('index');
actually this works
Route::get('/',[UserExpertController::class, 'index'])->name('index');
this doesn't work
Route::get('/','UserExpertController#index')->name('index');

How do I pass data from my controller to a view in Laravel 5.5?

I have a function that I'm trying to pass some data to a view. I can't get the Route to recognize the data, it just keeps saying my variables are undefined. Here's what I'm trying to do:
return redirect('confirm')->with([
'name'=>$name,
'service'=>$service,
'$email_address'=>$email_address
]);
Then in my web.php file, I have:
Route::get ('confirm', function($name,$service,$email_address){
return view('confirm',compact('name','service','email_address'));
})->name('confirm');
Laravel just throws an error "Missing argument 1 for Illuminate\Routing\Router::{closure}()"
I'm at a complete loss, I've tried this a bunch of different ways. If I call the view right from my controller, it works fine, but then URL isn't what I want it to be, so it seems like I have to return the redirect and reference the data in my route definition. Can anyone enlighten me?
If it's ok to show the email in the route then you can correct the problem like this :
Route::get ('confirm/{name}/{service}/{email_address}', function($name,$service,$email_address){
return view('confirm',compact('name','service','email_address'));
})->name('confirm');
And in the controller :
return \Redirect::route('confirm', [
'name'=>$name,
'service'=>$service,
'email_address'=>$email_address
]);
Ps : this is not recommended i just answer you problem but there is a better way to do it, passing just an id for exsample then find the user by id then take his email and other stuff.

Resources