For reference I have been working with this tutorial https://scotch.io/tutorials/build-a-time-tracker-with-laravel-5-and-angularjs-part-2.
I wanted to become more familiar with laravel 5 since I had previously only used 4 and found the above tutorial which also mixed in a little angular js. I followed part one and two of the tutorials to the letter and set up a database using mysql and phpmyadmin like directed.
I get to a section about halfway through which sets up a group route with the prefix api to pull seeded data from the database and display it in the view.
// app/Http/routes.php
...
// A route group allows us to have a prefix, in this case api
Route::group(array('prefix' => 'api'), function()
{
Route::resource('time', 'TimeEntriesController');
Route::resource('users', 'UsersController');
});
After this point I go to the page and the area that was previously rendered with data from a file instead of a database is now blank. If I inspect the element I get "failed to load resource the server responded with a status of 404 (not found)" and it displays my path time-tracker-2/public/api/time.
The routes work with these two controllers to populate the page with userdata from my database
// app/Http/Controllers/TimeEntriesController.php
...
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\TimeEntry;
use Illuminate\Support\Facades\Request;
class TimeEntriesController extends Controller {
// Gets time entries and eager loads their associated users
public function index()
{
$time = TimeEntry::with('user')->get();
return $time;
}
// app/Http/Controllers/UsersController.php
...
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
class UsersController extends Controller {
// Gets all users in the users table and returns them
public function index()
{
$users = User::all();
return $users;
}
Again I haven't worked much with Laravel and this is my first time messing with angular so I don't know if I am missing something super obvious or what the deal is. I have checked over all my code and compared it to the sample code and they are identical other than my database information. I have also scrapped the project and started from scratch and still get the same error when I get to this point.
Any sort of direction to look would be greatly appreciated because this error is driving me nuts.
Remember to point on the public folder, not on the root folder. That's why your URL is time-tracker-2/public/api/time and not time-tracker-2/api/time. This should fix your 404 error.
Related
I try to display a view but I get a 404 not found error, but my code seems correct
this is the route created
Route::get('talk',[\App\Http\Controllers\TalkController::class,'index'])->name('talk.index');
the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
class TalkController extends Controller
{
public function index(): view
{
return view('talk');
}
}
here is what I got as a result
enter image description here
need help please
I was Facing the Same Problem laravel Store cache and if you stop the serv and start it all over you will still see the 404 so its better to do :-
php artisan optimize
php artisan optimize creates a compiled file of commonly used classes in order to reduce the amount of files that must be loaded on each request. The file is saved to, or overwrites, bootstrap/cache/compiled. php , which needs to be writable by the web server (PHP process).
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 am having some trouble with my application when trying to create the user profiles. Here is the issue:
I am trying to create a view called userprofile.blade.php which will output any given users profile (based on id or username...doesn't really matter right now). Each profile page will show name, description, location, profile pic, and the given users posts. I used Laravel's Make:auth command to create the necessary authentication, customized the authentication forms, and then migrated all the columns I needed in my database.
My create and update methods work just fine (registering new users and updating their information). All the information is saved correctly in the database. However, I can only access it in my views with {{Auth::user()->}}. Whenever I try to use the Model in my Controller to access the data I need, it doesn't work. It seems to me as though I need to separate Laravel's default 'User' model with a custom model which I would call 'Account' or something along those lines.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Recipe;
class UserController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth', ['except' => [
'index', 'show'
]]);
}
public function index(){
$user = User::find($id);
return view('user.userprofile')->with('user');
}
I only included the index method from my UserController to keep it simple. It breaks down and tells me that 'id' in $user = User::find($id); is an undefined variable. That tells me that it isn't accessing my database table.
My question is, should I create a new fresh model that isn't mixed up with authentication to handle all the user profile information? If so, how do I access my current database table 'users' from this new model?
Please let me know if I need to clarify things for you guys. I'm not very experienced and I understand if my question is fuzzy. Thanks so much for your time!! I really appreciate any help I can get!
Hello and welcome to developing with Laravel!
You're on the right track - you need to identify which user's profile you're viewing, retrieve it from the database, and pass it to the Blade view. You don't need a new model or anything, though! Just need to complete what you've started.
You should start by defining a route parameter in your route, that will capture the dynamic data you want from the URL. Let's use the numeric ID for now. If you want a URL that looks like example.com/user/439, your route should look something like Route::get('user/{id}', 'UserController#index');.
Once you have that, the id parameter will get passed to your controller's method. Define it as a method parameter, and it'll be usable: public function index($id) { ... }
I think you can take it from there. :)
I just moved a project from laravel 4.0 to laravel 5.2. Am using a fresh installation of laravel 5.2 as suggested by Taylor. i have successfully transfer all files and web app works fine.
My challenge is that i have a page called page.blade.php that stores some value in session {{ Session::put('item', $itemSelected) }} base on the user choice and i have a Session::get('item') in my controller to receive this and process some logic.
The session returns null in the controller but when i add {{Session::get('item')}} in to page.blade.php it display value stored in $itemSelected. I also observe that session created in controller method can be access by the page.blade.php but the session created by the page.blade.php can't be access by the controller method.
Am a little bit confuse here, i need help. This process worked fine in laravel 4.0 before i moved to laravel 5.2
Here is my route
route::group(['middleware' => ['web']], function () {
Route::get('page', 'Website\PageController#mypage');
});
Page.blade.php
'''''
$itemSelected= 'Page 1';
{{Session::put('item' , $itemSelected)}}
Page 1
......
My Controller
<?php
namespace App\Http\Controllers\Website;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Authuser;
use View;
use Redirect;
use Session;
return
class PageController extends Controller {
public function mypage()
{
...
$selectedPage = Session::get('item'); //this suppose to return "Page 1" but returns null
....
}
}
I can't call this an answer to this issue but another way to get the job done.
From the view page.blade.php, I pass the value $itemSelected as a $_GET variable on a url. When link is clicked the controller picks the value from the request with Input::get('item'). Then persist the return value from request into Session (Session::put('item',$itemSelected). This got the task done.
Because it hasn't been set yet, you set it in the view, which is created after you run that line. I am guessing that is what the '...' implies.
Laravel routing functionality allows you to name a resource and name a controller to go with it. I am new to Laravel and would like to know if anyone knows how to extend the resources method in the route class provided.
Basically say I have: (which works fine)
/invoices
But say I want:
/invoices/status/unpaid
How is this achievable?
To see the basics of what I am doing check:
http://laravel.com/docs/controllers#resource-controllers
Resource controllers tie you into a specific URLs, such as:
GET|POST /invoices
GET|PUT /invoices/{$id}
GET /invoices/create
and so on as documented.
Since, by convention, GET /invoices is used to list all invoices, you may want to add some filtering on that:
/invoices?status=unpaid - which you can then use in code
<?php
class InvoiceController extends BaseController {
public function index()
{
$status = Input::get('status');
// Continue with logic, pagination, etc
}
}
If you don't want to use filtering via a query string, in your case, you may be able to do something like:
// routes.php
Route::group(array('prefix' => 'invoice'), function()
{
Route::get('status/unpaid', 'InvoiceController#filter');
});
Route::resource('invoice', 'InvoiceController');
That might work as the order routes are created matter. The first route that matches will be the one used to fulfill the request.