This is my first time asking questions in StackOverflow so sorry for the mistakes.
I have a problem with laravel routes.
<?php
use Illuminate\Support\Facades\Route;
//start :: login
Route::group(['namespace' => 'auth'], function () {
Route::get('/', 'AuthController#index')->name('index');
Route::get('/showResult', 'AuthController#Result')->name('Result');
Route::post('/showResultds', 'AuthController#showResult')->name('showResult');
Route::post('/dologin', 'AuthController#doLogin')->name('doLogin');
Route::get('/logout', 'AuthController#logout')->name('logout');
So these are my routes.
I have determined the app URL correctly in .env file.
My problem is that when my website is on the server (windows server) the route becomes the server's local IP address and not my domain or public IP address.
When I click on my links it becomes 172.30.30.4/login for example. and not domainname.com/login
Thanks for your help
When changing anything in any of the config files or the .env you should always run
php artisan config:cache
This will clear your current cache and cache your new settings
change the url in your config/app.php
'url' => env('APP_URL', 'http://localhost'),
and dont forget to run php artisan config:cache
Related
I have deployed my Laravel 8 application on live server and have set up the reverse proxy successfully to access the laravel app from a url like example.com/app/my-app.
Now the problem is, as I have used auth middleware which requests user to log in before he/she can access the app, opening example.com/app/my-app redirects to localhost:8000/login
Which is wrong as it is supposed to redirect to example.com/app/my-app/login
I have:
set the 'url' => "example.com/app/my-app" in config/app.php
ran php artisan optimize:clear
ran php artisan cache:clear
And also stopped the app and ran php artisan serve again
Can you please help me? Am I missing something?
You can use in web.php, if user not auth redirect '/' => '/login'
Route::group(['middleware' => 'guest'], function () {
// Redirect '/' => '/login'
Route::redirect('/', '/login');
// Auth routes
Auth::routes();
});
Remove the file inside the bootstrap/cache directory.Then run the following command :
php artisan cache: clear
php artisan config: clear
php artisan view: clear
php artisan route: clear
php artisan config: cache
php artisan optimize
Hope this will fixed your problems.
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');
});
I have a laravel project which I run from my local apache server directory.
The link for accessing this project is
www.localhost/project.dev/public/index.php
And I have a navigation menu
After I have set the APP_URL in .env file to
http://localhost/blog.dev/public/index.php/
I get no problems while navigating through the About and Contact pages in the project but when I access the Home page the browser goes to the
http://localhost/
but not to the
http://localhost/blog.dev/public/index.php/
How can I fix it? Here are my routes:
Route::get('/', 'PagesController#getIndex');
Route::get('about', 'PagesController#getAbout');
Route::get('contact', 'PagesController#getContact');
I think the best way is to setup a vhost in apache. if you want to work like this in your .env set the APP_URL= http://localhost/blog.dev/public/
Run php artisan serve in command line. The laravel buil-in server will active. And you will able to access your laravel application using correct url as like http://localhost:8000 try this and let me know if you have any problem.
I have a problem when I use routes in Laravel 5.0, I only need to call this route:
Route::get('home', 'HomeController#index');
But the browser shows me this, when I write http://localhost/course/public/home:
Not Found
The requested URL /course/public/home was not found on this server.
Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80
If I write http://localhost/course/public/
the index works but the other routes doesn't work, I don't know why.
In my Routes.php I have this:
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
I only downloaded laravel 5.0 with composer, I did not change nothing in the code after the download, I only want to test the routes and these don't work.
Thaks for your help.
in conclusion (the comments solved the problem, so thank you guys for this!!) you probably run Laravel on Apache and you don't have mod_rewrite enabled... or not configured correctly.
Laravel works with so called pretty urls. In order to achieve this, every request is rewritten and the config you can find in the .htaccess file in the public folder. This to give a quick and dirty explanation will let you write URLs without the index.php.
Additionally your server should serve the public folder and not the main folder. That's a security thing if you want to use your site in production.
Alternatively you can use nginx which in my opinion is a lot easier to configure and if you run laravel homestead everything comes straight out of the box.
I want to provide my customer with an option to configure their subdomain names in .env and config/app.php file. My Laravel route configuration is as follows:
Route::group(['namespace' => 'PublicSite',
'domain' => config('app.app_domain_public')], function () {
// public routes here
});
Route::group(['namespace' => 'AdminSite',
'domain' => config('app.app_domain_admin')], function () {
// admin routes here
});
It works fine and reacts to .env changes. But when I prepare my application package for deployment, I run php artisan route:cache and this makes the values of config('app.app_domain_x') frozen somewhere in bootstrap/cache/routes.php.
As the result, the application no more reacts to route domain changes in .env unless I run php artisan route:cache on the server. But there are situations when it's not possible to run artisan on the server.
Of course, I could configure .env as necessary before deployment but this breaks the idea of being able to modify .env settings without rewriting other application files (route cache, to be more specific).
Is there any workaround to make Laravel to cache at least some routes but still provide dynamic domain configuration?