How to visit my first route in Laravel? - php

I want to learn Laravel, so I just created my first project in my Sites folder. The folder in which it is in is now Sites/dump/laravel_test/. So following the quickstart docs I made a route in app/routes.php as follows:
Route::get('users', function() {
return 'Users!';
});
After saving the file I tried
http://localhost/~kramer65/dump/laravel_test/users
http://localhost/~kramer65/dump/laravel_test/public/users
but unfortunately, I get a 404 for both urls.
Since it's such a simple start, I don't really know what I should be checking. Could anybody give me some tips on what I could be doing wrong here?
[EDIT]
Turns out I had to give some permissions to app/storage/meta/serv‌​ices.json
I still wonder though, what are the most suitable and safe permissions to use here?

Related

Laravel getting 404 error when creating new route

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!

Laravel App - Admin Route ONLY '/admin' will always return a 404 error | '/admins' or anything else works

So if you go here you will see the EXACT SAME ISSUE word for word that i'm having...
Basically I have the admin route set like this:
Route::get('/admin', function () {
return view('admin.app');
});
Simple enough right? It always returns this error here:
Not Found
The requested URL /admin/ was not found on this server.
If i change the route to /admins (plural) or really anything else, even /test the route displays properly.
I DID have an admin folder inside my public folder, but I have since deleted the admin folder and now the problem is still persisting. I don't really understand why. I'm running on a Laravel/Homestead environment. Basic LEMP stack. Nothing special.
Edit: When i type in mysite.dev:8000/admin it directs me automatically to mysite.dev/admin (Notice there is no port) So, now i'm really even more confused. :/
Edit 2: Changed the route to admin/dashboard works just fine. Could it be possible that Laravel has defined the /admin route as a 'reserved route' or something (similar to reserved keywords in any programming language) to where it won't ever display anything? Is that even a thing?
Route::get('admin/dashboard', function () {
return view('admin.app');
});
Any ideas?
delete or rename admin folder in public and run
php artisan cache:clear
I think you have admin name folder in your public directory if you remove that it will work enter link description here

Can't route properly in Laravel

I am very new to laravel and I simply don't get how to route properly, am completely lost. The problem is I can only route a view to localhost/public. For example I can't route a view to "localhost/public/something". Am not even sure my urls are correct. If i leave "something.php" empty nothing will show, but if i add some html it shows only the html no views. Am sure am doing it all wrong. So how does one route a view to a page other than "public/".
The routing for index is
Route::get('/', function(){
return View::make('welcome');
});
works okay.
Now how can I achieve the same if do
Route::get('something', function(){
return View::make('welcome');
});
or
Route::get('/something', function(){
return View::make('welcome');
});
So i finally got it to work after so many hours. I deleted my laravel installation and did a new one and the problem is gone. Thanks.
There's nothing wrong with your code but might be for .htaccess
Please try accessing the route via following URL
http://localhost/public/index.php?/something
if it's working, you need to use the alternative .htaccess file provided on Laravel website or see if mod_rewrite is loaded on your Apache.

Laravel not getting resource?

I am a noob with laravel just picked it up today, what seems to be an easy task that looks lovable has caused a headache.
I am following a tutorial from Jeffery Way, and we seeded the database, the next step is to create a controller and get the resource. I generated the necessary files, then followed these basic steps.
The database table is 'players'
Step 1.
//Here is the index() function inside my PlayersController.php
class PlayersController extends \BaseController {
public function index()
{
return Player::all();
}
}
Step 2.
//Inside the app/models/ directory I create a file called Player.php
//Here is the code inside Player.php
class Player extends Eloquent {
protected $table = 'players' // I tried without the protect aswell
}
Step 3.
//Finially a route
Route::resource('players', 'PlayersController');
Route::get('/', function()
{
return View::make('hello');
});
For extra measure I also ran composer dump-autoload in the command line, inside the root directory of my laravel project.
Then now I wasnt told which directory the route would be in, since it's an index function I assumed it's inside the root, so I have tried several url's
http://localhost/basketball-app-framework/public/players
http://localhost/basketball-app-framework/players
This last one would obviously only work if I pointed localhost to the public directory, I did do that however, still a no go. I am confused as ever.
http://localhost/players
I have tried a bunch other url's but those only are the ones that make sense, and the ones I am pretty sure should be working.
It blows I have to do some digging to figure out this simple mistake I am sure I am making, but whatever it takes to understand why I cant fetch that data from my database.
Okay looks like it was a mod_rewrite, and AllowOverride All issue inside the http.conf file.
I figured out some better keywords that applied to my issued and came across this Laravel 4 simple route not working using mod_rewrite, and .htaccess
As of now the simple users route that the documentation provides works. http://localhost/basketball-app-framework/public/users returns Users!.
So its nice to have this figured out, I still feel confused though haha.

Intergrate Laravel4 into an exisiting flat PHP site

Would like to integrate Laravel4's into an existing flat PHP-MySQL site,
while I am applying MCV logic page-by-page I need to keep this site work normally.
Before moving ahead: Q1.Does this intergration work at all ? Q2.Does anyone foresee any problem ?
Sofar I have only done this:
app/routes.php
Route::get('/', function(){
//just keep empty, index.php shows up as intended.
});
public/index.php
test
<?php
//Codes transplanted from Laravel4
//.......start......
/**
* Laravel - A PHP Framework For Web Artisans
*
require __DIR__.'/../bootstrap/autoload.php';
It won't quite work this way. In the middle of MVC we have our "Views" which are what laravel will return to the user for display.
What you can do, though, is put your index.php folder in the app/views directory. If, for example, you put the content of your index.php file in:
app/views/index.blade.php
You can then call it via:
Route::get('/', function(){
return View::make('index');
});
This will have the added benefit of getting you through your first step of moving from a "flat" PHP site and in to the framework.
Note: If you DO try using the implementation you provide in your example, you're going to get a bunch of "Not Found" exceptions.

Categories