New routes in Laravel are not working - laravel-5

I just tried to add a new route in Laravel but it seems it's not working, just getting 404 errors.
It only detects the index '/' route.
This code is in the routes/web.php
Route::get('/', 'SiteController#mainView')->name('home');
Route::get('secondroute','SiteController#secondRoute')->name('secondRoute');
The Controller is also working, because it doesnt matter if the index Route (/) is the mainView or SecondRoute so it has to be something with the routing itself?
Thanks
Edit: Mod Rewrite is on
Edit2: I'm using a Ubuntu on a Virtual Machine with Apache 2.4.25
Edit3:
public function secondRoute(){
return view('myself', ['title' => 'Myself']);
}

You missed '/' in second route
change
Route::get('secondroute','SiteController#secondRoute')->name('secondRoute');
to
Route::get('/secondroute','SiteController#secondRoute')->name('secondRoute');

Related

Adding subdomain in Laravel 9

For some reason adding subdomain route isn't working and keeps on returning my homepage instead of the page I need. I'm using Laravel, Inertiajs Vue in my app.
Here is my route:
Route::domain('webshopview.localhost:8000')->group(function () {
Route::get('/webshopview', function () {
return Inertia::render('Products/Index');
});
});
I want to be able to access it as webshopview.localhost:8000 but every time I try to visit this route it returns my app home page. If I do this in normal route like the below example, it works like a charm.
Route::get('/webshopview', function () {
return Inertia::render('Products/Index');
});
What is missing to create a subdomain for group of routes? Why it keeps returning the app homepage ignoring the subdomain
My app works locally on 'http://localhost:8000/' and 'http://127.0.0.1:8000/' and it works like that by default without me doing anything. So all I want is to add subdomain to specific routes so that I can access like this 'example.localhost:8000' or 'example.127.0.0.1:8000'. The end result is to have the route displayed like this after deployment 'https://www.subdomain.domain.com'
I even tried this and it didn't work:
Route::get('/productview', function () {
return 'First sub domain';
})->domain('productview.localhost');
now accessing 'http://productview.localhost/' returns 'This site can’t be reached' error.
I had to manually add the routes in my etc/hosts file on windows and restart the entire machine for it to take effect.

Route [add.sp] not defined

i wrote url like that
use App\Http\Controllers\DashController;
Route::get('/admin/special', [DashController::class, 'addSpecializations'])->name('add.sp');
this is controller
public function addSpecializations()
{
return view('dashboard.add-specializations');
}
when i tried to open it i can't even though all route work
after that i wrote this code in view's file
<a href="{{route('add.sp')}}">
so i faced this issue
Route [add.sp] not defined.
In case your routes are cached, run php artisan route:clear. In development, don't cache anything, including views and config.

How to get previous route name in Laravel 5.8

I'm trying to get previous route name in Laravel 5.8. This answer (https://stackoverflow.com/a/40690569/16735772) works perfectly when I use Virtual Host but in direct url like (125.125.125.125/project_folder/public) does not because NotFoundHttpException is thrown.
With Virtual Host, $request->create(url()->previous()) creates a request with pathInfo and requestUri using relative url, for example, my_profile/2 but without Virtual Host, those attributes have different value, like project_folder/public/my_profile/2.
I don't need to redirect back, just know previous route name to check. For example:
if ($previous_route == 'show_name') {
//do something
}
Any help will be appreciated.
Try this code to get the previous route name:
Route::getRoutes()->match(
Request::create(URL::previous())
)->getName();
You can make a helper or blade directive to make it easier to access.

Exclude conditional routes in Laravel

I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');

Working with Laravel routes

I built my application using Angularjs on the frontend and Laravel 5 at the backend, however my main issue now is routing, when the page is loaded initially I set it to return my angular.php view I even added some code to catch all routes and return that view for me.
This does not work in all cases:
routes.php
Route::any('{url?}', function($url) {
return view('angular');
})->where(['url' => '[-a-z0-9/]+']);
Example of a URL that works with this is:
http://localhost:8000/tickets/events/catgeories/
Example of a URL that does not work with this is:
http://localhost:8000/tickets/events/Musical/Some-event-name
By "not working" I mean Laravel throws a NotFoundHttpException. What I am thinking right now is the above route can't go past three levels/parameters as in /level-1/level-2/level-3.
What am I doing wrong here?
Maybe because second URL has uppercase characters?

Resources