Laravel routes do not work - laravel

I am still new to PHP Laravel framework. The problem is that my Laravel routes do not work. Here is the simple code of my routes.php:
<?php
Route::get('/', function()
{
return View::make('hello'); //shows "Hello" page
});
Route::get('somepage', function()
{
return View::make('somepage'); // Apache server shows 404 error
});
I have configured my Apache param AllowOverride All and the mod_rewrite module is loaded. I have another installation of Laravel which works (throws 'NotFound' exception on undeclared route), but this one does not respond properly, on the same server. Where is the problem?

Make sure that your .htaccess file is setup correctly.
http://laravel.com/docs/installation#configuration

Please configure your Apache param AllowOverride All and ensure the mod_rewrite module is loaded.
http://laravel.com/docs/4.2/installation#pretty-urls

Related

How to set up a Laravel App with Subdomains to work on a cPanel server?

First of all: this is not a duplicate of (How to set up a laravel project on cPanel subdomain?) this question isn't the same as mine.
I would like to know how can i set up a whole Laravel application, with subdomains (not only one subdomain) to work on a cPanel server
So here is the test code i'm using on my routes/web.php file.
Route::domain('test.mydomain.com')->group(function() {
Route::get('/', function() {
return 'DOMAIN TEST.MYDOMAIN.COM';
});
});
Route::domain('mydomain.com')->group(function() {
Route::get('/', function() {
return 'DOMAIN MYDOMAIN.COM';
});
});
Route::get('/', function() {
return 'ROOT DOMAIN';
});
I'm currently using cPanel to handle domains and subdomains, but i have no idea how to make it work with Laravel Subdomains, i tried the code above.
When i access my main domain mydomain.com it shows "DOMAIN MYDOMAIN.COM" so it works ok, but when i go to test.mydomain.com it just access my subdomain folders from this subdomain, so, how can i make it works?
Maybe i need to put some .htaccess file to get this working properly? Can someone help?
Did you try to link subdomains to the same folder as primary domain ?
Eg.
domain.com => /public_html/public
test.domain.com => /public_html/public

404 Not Found on sanctum/csrf-cookie path

So I've been building a projet based on laravel. I'm building on a SPA foundation with sanctum as my authorization package. It works perfectly. Then I deploy the project to the server and everytime I try to login there is a 404 error on /sanctum/csrf-cookie.
How could this happen? Is it because the SanctumServiceProvider not working.
The problem is when you define sanctum prefix, the route become something else like this:
you can check your routes with : php artisan route:list
as you can see the /sanctum/ is removed and when you check the route /sanctum/csrf-cookie it will not be and throws 404 error. So you have two options:
add this prefix: 'prefix' => 'api/v1/sanctum'
or
change GET call to api/csrf-cookie
You need to check if you're setting correct axios defaults for your /sanctum/csrf-cookie api call.
You can set it as follows
axios.defaults.withCredentials = true;
axios.defaults.baseURL = "http://localhost"; //Before sending get request
axios.get("/sanctum/csrf-cookie").then(async () => {
//Logic to handle login
});
If defaults are not set properly url becomes http::localhost::8080/sanctum/crf-cookie which is where frontend is serving but instead it has to be http::localhost/sanctum/csrf-cookie
Note: localhost example is just for explanation. For production server make sure your url is correct and api call is on backend.
I solved this issue by adding:
AllowOverride All
to the apache directory config
add in last line inside config/sanctum.php
'routes' => false,
Add in config/cors.php
'paths' => ['*']

How can I handle NotFoundHttpException in Laravel 8?

I'm developing API in Laravel 8. I want to handle NotFoundHttpException to json response. But App\Exception\Handler updated in Laravel 8. By default render method does not exist. So I do not understand what I want to do. I have read in Laravel docs.
You can use a custom callback as per the documentation, App\Exceptions\Handler:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
public function register()
{
$this->renderable(function (NotFoundHttpException $e, $request) {
return response()->json(...);
});
}
If you send the correct accept header with your request the exception handler will return a JSON response already though.
Laravel 8.x Docs - Error Handling - Rendering Exceptions
if you are running on Xamp , edit vhosts where lvauth is your project and directory name
DocumentRoot "C:\xampp\htdocs\lvauth\public"
ServerAdmin lvauth
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
if you are running on Xamp , edit vhosts where lvauth is your project and directory name
VirtualHost lv.auth:80
DocumentRoot "C:\xampp\htdocs\laravel\public"
ServerAdmin lv.auth
<Directory "C:\xampp\htdocs\laravel">enter image description here
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Directory
VirtualHost

How can I use sub-directory?

I am new and install Laravel 6.x. And then I got a welcome(default) page.
But I didn't use the sub-directory in the website URL
I created the sub-directory like this below.
"www.test.com/test"
But that is not working.
/routes/web.php code source
It is working well.
Route::get('/', function () {
return view('./posts/test');
});
It is not working. So I got the "404" page.
Route::get('/test', function () {
return view('./posts/test');
});
How can I use sub-directory?
The problem was the Apache configuration:
<Directory "/home/....../public">
AllowOverride all
</Directory>
(posted as a community wiki-answer because this question has been answered in the comments)

Controllers and ajax in laravel 4

I'm new at Laravel and I can't figure how to handle controllers (and ajax).
I have a button in a sidebar, and I want to show a page when it's clicked.
I have a view (which is the page i want to display in ajax) located in views/logs/system.blade.php
and a controller located in controllers/LogsController which has the following code -
class LogsController extends BaseController {
public function getLogs() {
return View::make('logs/system');
}
}
my routes.php has the code -
Route::get('/', 'HomeController#showWelcome'); // Works fine
Route::get('logs', 'LogsController#getLogs');
First things - how can I access the view I'm gettings in getLogs in a URL (localhost/mysite/public/logs doesn't work...)
Second - how can I access it in an ajax call?
I tried
$.get('logs', function(data) {
console.log(data);
});
but it doesn't work either. It gets 500 Internal Server Error....
Help please!
You should be able to go to: localhost/mysite/public/logs
if not, enable mod_rewrite in your apache and in your apache httpd.conf, set:
AllowOverride All
Most probably the server error 500 (in both cases) is caused by the fact that you have a mistake in the View::make() call. To utilize a view in subfolder you have to use the dot notation.
So correct the code
class LogsController extends BaseController {
public function getLogs() {
return View::make('logs.system');
}
}
and you should be good to go, the url should load fine, both in browser and in Ajax.
If you still have problems, check the Laravel Logs (probably path/to/app/storage/logs/...) as well as Apache Error Log (probably /var/log/apache2/error.log). I assume you are using Unix/Linux operating system.
The rewrite module was on.
I solved it by going to localhost/mysite/public/index.php/logs, this is the URL that it expects, maybe something in the .htaccess file is wrong.

Resources