I am very confused as to why this is happening but here is the issue guys. Basically I am building an app using laravel4 I want to go to this URL
http://app.tld/books
However I get this error every time:
404: The requested URL /books was not found on this server.
However in my routes.php file I have the route defined:
Route::get('/books', 'BookController#index');
I also have a view and relevent method in Controller:
public function index(){
$books = Book::all();
return View::make('book.index')->with('books', $books);
}
From the comments we gather that it was an Apache issue, not a Laravel issue (mod_rewrite wasn't enabled -- kudos to #watcher), and I'm glad you sorted it out.
For future reference, it seemed likely to be an Apache issue because Laravel throws PHP exceptions on undefined routes, namely a Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException. If you have debug set to true you'll get a neatly styled stack trace with code highlighting and server/request data, if not you'll get a neatly styled "Whoops, looks like something went wrong." message. If, on the other hand, you get a white screen with Times New Roman messages like "404: The requested URL /books was not found on this server.", Laravel isn't even starting up.
As the comments on your question show, there are many other clues which will tell you that it's not your Laravel routes, though from the code you posted and even the title of the question, you seemed to had narrowed it down to routes, which probably had you looking in the wrong place for a while.
Related
For laravel API I have write the test cases. But whenever I am running the test cases it always failed with below error,
1) Tests\Feature\CompanyTest::orgTest
Expected status code 200 but received 404.
Failed asserting that 200 is identical to 404.
After adding the $this->withoutExceptionHandling(); to testcase code it return the below error,
1) Tests\Feature\CompanyTest::orgTest
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST domainname/index.php/api/company
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php:126
/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:415
/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:113
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:507
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:473
/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:332
/tests/Feature/CompanyTest.php:21
My Code in Test File is,
public function orgTest()
{
$requestData = ["organizationId" => 10,"offset"=>1,"limit"=>10,"notificationId"=>""];
$response = $this->withoutExceptionHandling();
$response->postJson('/index.php/api/company',$requestData);
$response->assertStatus(200);
}
I have googled the error and tried many solutions but unable to succeed. Anyone please let me know whats the issue.
It is a 404 error, so your webpage is not found :
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: POST domainname/index.php/api/company
Bad way to go, post to /api/company or with /index.php/api/company but not with "domainname" !
If it is not working, check your route config for that api route. Are your controller and action declared well ?
By default, laravel's routing does not explicitly show that it is a php file rather than it has dedicated endpoints which you decide on the routing file. So index.php shouldn't be a valid route for laravel.
A 404 error status code refers to a Not Found Error. Maybe the api url that you are trying to access does not match the one inside the routes file whether you are using web.php or api.php.
I suggest using php artisan route:list on the base path of the project to properly see that the route you wanted is registered and match it from there.
It seems to me that PHPUnit can't find not only this URL but the whole website. You could check it by request to "/" in your test.
If I'm right, first of all, I'll suggest checking the param APP_URL in your .env.testing. Usually, I set it as APP_URL=http://localhost, and it works for me.
A 404 code means there was nothing found at the specified route. It could be the route you specified index.php/api/company. Try routing to [host:port]/api/company
it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!
I'm having an issue where a route is returning a blank page. I am using Homestead as my dev environment and I'm unsure how to debug.
The /storage/logs/laravel ... isn't returning any exceptions when I visit the white page.
web.php (where it's failing):
Route::get('/clinic/register', 'ClinicController#register');
Controller.php:
public function register()
{
return view('clinic.register', ['specialisms' => Specialism::pluck('specialism', 'id')]);
}
Yet when I visit /clinic/register I am shown a blank white page. How can I see why it's failing? Surely a white page will return an exception somewhere?
As you have not provided your entire route setup. This answer is my best guess. See if it helps.
Your issue hint at improper route setup. If you have created a clinic resource then clinic/register route should precede it.
// clinic/register route should come first
Route::get('clinic/register','ClinicController#register');
// followed by rest of the routes which resource will create
Route::resource('clinic','ClinicController');
The reason behind getting a blank pages is because Route::resource will create some route with wildcards.
For e.g. clinic/{clinic} which will map to show method on controller. So when you make a get request to clinic/register it will be mapped to this show method instead of your register method.
One possibility for not getting any errors is your show method does not have any code yet. Hence, a blank response.
To summarize: Order in which you register your routes matters
I have the following resource route:
Route::resource('pools', 'PoolsController');
I also have an edit form which should post to the controller's "update" method, set up like this:
{{ Form::open(array('route' => ['pools.update', $pool['id']])) }}
When I submit the form, it opens www.domain.com/pools/6 (6 being $pool['id'] above). However, instead of running the code in the update() method, it throws an error:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Now, I've found Laravel's error reporting very unhelpful so far, and this is no exception. The error description is vague at best and does nothing to help me troubleshoot the issue.
I was under the impression that the update method should receive post data automatically when using resourceful routing. It has also worked in some examples before, using the same syntax.
So, can anyone tell me what might be going on here?
to run the code in the update method, you must spoof a PUT request. look here: Form Method Spoofing
Can anyone link me to some Symfony resources, they are hard to find. I am having a little trouble understanding how to use it correctly. Like with CodeIgniter to use the security helper you would load it doing this:
$this->load->helper('security');
And you would use its functions you would do something like this:
$data = $this->input->xss_clean($data);
But with Smyfony to redirect someone to a 404 page, you need to use the sfAction class and the redirect404() api. So could anyone explain or link me a good tutorial?
I would highly recommend you set aside a few hours and read through the Practical Symfony tutorial. It goes through all the basics from project start to end.
Symfony, although a great framework, has a steep learning curve. This tutorial really helps you understand how it works, from the basics to more advanced stuff.
http://www.symfony-project.org/book/1_2/06-Inside-the-Controller-Layer#chapter_06_sub_skipping_to_another_action (scroll down a few paragraphs)
Using
http://www.symfony-project.org/api/1_4/sfAction
You have access to a number of 404 redirection methods (redirect404, forward404, forward404If, forward404Unless) which are detailed on the link above. You have access to all of these methods from within your actions:
public function executeAction(sfWebRequest $request)
{
$this->forward404();
}
I would recommend using forward404 instead of redirect404 as the latter will push the redirection back to the browser and show your user the 404 page's URL instead of the URL they attempted to access.
Configuring
You can configure the module and action that should be executed when a 404 is triggered in your application's config/settings.yml like this:
all:
.actions:
error_404_module: my_module # To be called when a 404 error is raised
error_404_action: my_action # Or when the requested URL doesn't match any route
Update:
For general information on Symfony check out the three books they have available online: http://www.symfony-project.org/doc/1_4/, 'Practical Symfony' being a great place to get started. There is also a complete API reference available: http://www.symfony-project.org/api/1_4/.