laravel invalid route action with subdomain - php

What is the proper way to add a subdomain into your routes? I have a laravel/homestead project working on my local computer but when I move it to HostGator, a shared host server, it no longer works. Well the home page works but the links to the sub-pages don't.
For example, the following route works fine on my local version:
Route::get('/appointments', 'AppointmentController#index');
But if I use that route on the remote server, it takes it to tekknow.net/appointments instead of tekknow.net/medaverter/appointments.
So I followed instructions from here:
https://laravel.com/docs/5.6/routing#route-group-sub-domain-routing
and added a prefix like this:
Route::prefix('MedAverter')->group(function() {
Route::get('/appointments', 'AppointmentController#index');
});
But that doesn't work either. It also goes to /tekknow.net/appointments
I also tried changing the route to this:
Route::get('/MedAverter/appointments', 'AppointmentController#index');
But that also went to tekknow.net/appointments
Anybody know what I'm doing wrong?
EDIT: I went onto my HostGator cPanel and looked at all my subdomains and saw that my subdomain root was medaverter.tekknow.net which is linked to Document root of medaverter.tekknow.net/MedAverter which gets redirected to http://www.tekknow.net/MedAverter. So I renamed my folder from medaverter to MedAverter to match the subdomain redirection.
Here is a screenshot showing what I see in cPanel for columns Subdomains.Root Domain, Document Root, and Redirection

When you try php artisan route:list | grep appointments,
it prints:
[~/www/MedAverter]# php artisan route:list | grep appointments
| | GET|HEAD | MedAverter/appointments | | App\Http\Controllers\AppointmentController#index | web |
That means your route MedAverter/appointments is working for laravel.
Error 404 means route cannot be found.
So I think that's something wrong with your nginx configuration.
When I try http://tekknow.net/MedAverter/MedAverter/appointments.
It has really found the route with error 500.
So, I think you have defined this code in your nginx configuration:
location = / {
rewrite ^ https://tekknow.net/MedAverter;
}
Solution 1:
Change back to rewrite ^ https://tekknow.net/; in nginx configuration.
(I'm not familiar with hostGatar cPanel, but I think you can change medaverter.tekknow.net/MedAverter redirected to http://www.tekknow.net/`)
And in your laravel project, you need to keep prefix MedAverter to appointments.
Solution 2:
Keep the rewrite code. It means you don't need to change the cPanel redirect.
And remove prefix MedAverter in laravel routes. HostGatar(Nginx) will automatically redirect with this prefix MedAverter for appointments.

Clear all caches
The problem you're getting can be due to cache. So, make sure that's not the problem by running
php artisan route:clear && php artisan view:clear && php artisan config:clear && php artisan cache:clear && php artisan clear-compiled
Base URL
If you want to change the base URL of your Laravel app, that's something already asked.
Multiple Path Prefixes
In this case you have a more than one route with the same prefix in their path. If that's the case, you can use route groups to simplify the structure.
Route::prefix('medaverter')->group(function () {
Route::get('/', function () {
// Path /medaverter
});
Route::get('appointments', function () {
// Path /medaverter/appointments
});
});
This would go to, respectively, tekknow.net/medaverter and tekknow.net/medaverter/appointments. You could also create another prefix, like testing, configure in the same way and go to tekknow.net/testing or tekknow.net/testing/whatever.
Namespace prefixes
When you’re grouping routes by route prefix, it’s likely their controllers have a similar PHP namespace. In the medaverter example, all of the medaverter routes’ controllers might be under a Medaverter namespace.
Route::namespace('Medaverter')->group(function () {
// App\Http\Controllers\Medaverter\AppointmentController
Route::get('medaverter/appointments', 'AppointmentController#index');
});

u can use route groups for that here is a sample try this:
Route::group(['prefix' => 'medaverter'], function () {
Route::get('/appointments', [AppointmentController::class, 'index'])->name('index');
});
or
Route::group(['prefix' => 'medaverter'], function () {
Route::get('/appointments', 'AppointmentController#index');
});

Related

Laravel 6 not returning 404 when calling a not existing route

I have some issues with Laravel. I called php artisan route:cache and then the issues began. For example, the Auth routes (login etc.) can be called even if the User is logged in. Then the caching command seems to not clearing the routes. I noticed it because I put my Auth routes in the middleware guests because of the rendering of the Auth routes. After I ran route: clear it worked.
Also, the 404 routing doesn't work since that, because if I call a route that doesn't exist, then the Symfony Framework throws an error:
Symfony\Component\Routing\Exception\ResourceNotFoundException
That's my web.php:
Route::get("/installer","install\InstallController#index");
Route::group(["middleware"=>"guest"],function(){
Auth::routes();
Route::post("login","Auth\Logincontroller#authenticate");
});
Route::group(["middleware" => "auth"], function () {
Route::get("/logout","Auth\LoginController#logOut");
Route::get('/', "dashboard\DashboardController#index");
});
Also, the installer route doesn't work. I will always get redirected to localhost/dashboard (even if I change the route name). My domain for the Laravel is called raptor.debug, so I don't know why it's redirecting to localhost.
Can someone point out what I did wrong or is this a bug?
As Anas Bakro pointed out, the command php artisan route:cache will brick the app, when the folder of the controllers are lowercased.

Route/View related issue in Laravel

I've encountered a problem while creating simple apps with Laravel on IIS.
When I create a new Laravel application I can see the Laravel welcome page just fine.
If I create another view in the same folder as the welcome.blade.php (test.blade.php for example), and set up the route for that in routes/web.php I can't navigate to that page in browser. EDIT: When I attempt this I get a 404.
My web.php is as follows:
<?php
Route::get('/', function (){
return view('welcome');
});
Route::get('/test', function (){
return view('test');
});
At first I thought that perhaps the project was not reading web.php, but when I run php artisan route:list the test route is listed.
I thought perhaps that my view didn't work, so I renamed it as welcome.blade.php and that loaded up fine. I just seem to be unable to add a route to any view or anonymous function that isn't mapped to welcome.blade.php
I tried adding a static route.php file into the app directory with the same code, but that made no difference to the result.
I'm sure I must be missing something basic, but I can't seem to put my finger on where I've gone wrong. Would massively appreciate any help you might be able to offer. Thank you.
Right I worked out where I went wrong. I hadn't run artisan serve. Very simple.
If you're having this problem, try navigating to your application root in cmd and then try
php artisan serve
Then navigate to your page using that information.
Thank you to everyone who posted, massively appreciated.
You need to install Url Rewrite in your terminal. Then import the .htaccess file located in your project/public folder.
To install Url Rewrite, you need to install Web Platform Installer (WPI). Once installed, open WPI then search for the keyword "Url Rewrite", then install the first item in the result. Once the download is finished, Url Rewrite will be available in your IIS Manager
I think IIS server maybe blocks test url.
IIS server has such features.
Before time, I remember that I used IIS blacklist url feature.
You may check IIS url block feature.

Laravel 5.4 running all routes through subdomain

I have just deployed Laravel 5.4 using Composer on a shared hosting and I've run into several problems which I think are now fixed.
The first one was folder permissions which is now fixed so at least I see a welcome page.
I noticed that composer install never create a routes directory so I uploaded a local version and also it never creates a cache directory in the boostrap folder which I fixed also.
I have installed Laravel on a subdomain and I want everything to run through the subdomain as the root folder has a different application.
If i got to the subdomain, the welcome page opens as expected but as soon as I try to use a different route, I get this error:
NotFoundHttpException in RouteCollection.php line 145:
I have looked at what other people have done with subdomains but nothing works for me. I don't know if there are other files are missing during the install
This is what I have in the Routes.php
Route::group(['domain' => 'subdomain.example.com'], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('test', function () {
return view('welcome');
});
});
I assume every route needs to run in the subdomain route group or do I need to use this at all seeing as the install is in the subdomain directory and the vhost is pointing to the subdomain public folder
It turned out to be a problem with the PHP version the command line was using on Plesk.
Currently Plesk runs in 5.4 and Laravel needed 5.6. For some reason Composer ran successfully and it seemed to miss downloading some of the files.
I wiped everything from the subdomain and thanks to this http://blogs.reliablepenguin.com/2015/08/18/using-php-composer-phar-with-non-default-php-install i was able to run composer using php version 5.6
I didn't need any special route for subdomains as everything is in the subdomain and these routes worked as expected
Route::get('/', function () {
return view('welcome');
});
Route::get('test', function () {
return view('welcome');
});

Laravel 5.3 route not working

I have installed fresh copy of laravel 5.3.
I have the following code in my route/web.php.
Route::get('/', function () {
return view('welcome');
});
Route::get('welcome', function () {
return view('welcome');
});
when i hit localhost/project/public in the browser i can see laravel welcome page.
But when i hit localhost/project/public/welcome then 404 Not Found comes up where i should get the same laravel welcome page.
Am i forgetting something ?
Laravel is case insensitive, so if you created the project with one of these will not find the path.
Has your route file been cached? See what happens when you run:
php artisan route:clear
and try again.
I was having some trouble with this myself, so for anyone having trouble with getting a Laravel route to work:
Make sure your route is defined the correct way by running php artisan route:list Your route should show up, along with the method. (get, post, ...)
Enable mod_rewrite from the Apache settings so Laravel can map /uri to whatever you like.
Modify the .htaccess or Apache settings to allow url overrides: AllowOverride All If you are working on something like localhost/~username/yourproject, also check the username.conf file in /private/etc/apache2.
I'm not an expert in Apache settings, so feel free to correct or elaborate where needed.
goto htaccess in your laravel folder
after ( RewriteEngine On ) add this
RewriteBase /yourlaravel project name/public
example:
RewriteBase /laravel/public

Laravel base route errors "NotFoundHttpException"

I'm too newbie to Laravel...I have written this route to echo "Hello World", but It errors NotFoundHttpException
This is my routes.php (no other code is in the file but the following):
Route::get('/', function(){
return "HELLO WORLD";
});
I have also enable mode_rewrite, and also set AlloOverride to 'all' in apache module.
This is also the URL is use to access the page:
http://localhost/laravel/public/mostafa
Do:
php artisan serve
Use that URL to visit your website.
You will see that you get some output like:
Laravel development server started on http://localhost:8000
When you have no clue what Artisan is take a look at this;
http://laravel.com/docs/artisan
Navigate into your project directory with your command prompt and run there the serve command from above.
Example:
Sometimes the default .htaccess file located in public folder doesn't work in apache. Try altering the .htacess as mentioned here. Alternatively renaming your laravel project folder would work especially in XAMPP.
Some other frameworks treat route definitions as relative to the project web root so a path defined as /foo/bar will match http://example.com/additional/levels/laravel/public/foo/bar and will work without changes even if you move the project tree somewhere else in your public web hierarchy. Laravel, however, considers routes as absolute paths so /foo/bar will only match http://example.com/foo/bar.
The simplest solution is probably to move your code into a separate virtual host and point its document root to laravel/public/. (In this case it seems that's actually the intended set-up.)
(I suppose there's a way to make the framework assume an implicit prefix but I've only been using Laravel for 10 minutes.)

Categories