I have a Laravel 5.2 instance utilizing all the typical out-of-the-box routes for dashboard, cases, home, login/logout, and users (which is working well). I now need to create a wizard with steps (step1, step2, step3, etc.), and I need access to the session. I assigned them to the group middleware.
Route::group(['middleware' => 'web'], function () {
Route::get('/', function () {
// Uses Web middleware
});
Route::get('wizard/step1', [
'as' => 'wizard/step1', 'uses' => 'Wizard\WizardController#getStep1']);
Route::get('wizard/step2', [
'as' => 'wizard/step2', 'uses' => 'Wizard\WizardController#getStep2']);
});
However, when I go to the named route(s), I get a 404 error. So WizardController looks like the following.
namespace App\Http\Controllers\Wizard;
use App\Http\Controllers\Controller;
use App\Http\Requests;
class WizardController extends Controller
{
public function __construct()
{
//$this->middleware('guest');
}
public function getStep1()
{
return view('wizard.step1');
}
}
The defined views are resources/views/wizard/step1.php. Ideally, I'd like to refactor it so that Wizard is a separate group. However, nothing seems to work with the way I'm defining custom routing currently.
This happens when you cache the routes. The new route entries you add will never be recognized unless you clear the route cache.
You can delete cached routes using php artisan route:clear.
Since you will be changing the routes frequently in dev env, It is always better to not perform route caching while in dev environment.
You can do it by only running artisan route:cache as a post-deploy hook in Git, or just run it as a part of your Forge deploy process. So that every time you deploy your code in your server, your routes are cached automatically.
I resolved my issue by running the command:
php artisan route:clear
This lets Artisan delete your route cache and you can you can make sure routes are in sync.
Thanks for the help!
Related
I have Laravel with Jetstream installed. How can I add a route to Fortify?
I've read through the whole readme:
https://github.com/laravel/fortify/blob/1.x/README.md
That readme provides ways to customize functionality but it doesn't show a way to add a new route to Fortify.
I can see the routes.php file in
/vendor/laravel/fortify/routes/routes.php
but you're not supposed to edit stuff in the vendor folder. If you edit anything inside the vendor folder, whenever you run a Composer update it will overwrite any of your changes when the files update.
Typically I think you'd have to do some sort of artisan command to get proper access to the corresponding files by publishing Fortify's resources like:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
This would publish Fortify's actions to your app/Actions folder, etc
How can I add a new route to Fortify in the right way?
You should never touch or mess with vendor as it is immaculate.
By default the fortify routes located on /vendor/laravel/fortify/routes/routes.php, but you shouldn't edit anything inside the vendor folder otherwise whenever you run composer update it will overwrite any of your changes when the files update.
You can simply do the same on /routes/web.php with fortify middleware :
Route::group(['middleware' => config('fortify.middleware', ['web'])], function () {
// with fortify guest middleware
Route::get('foo', function () {
return 'Foo';
})->middleware(['guest']);
// with fortify auth middleware
Route::get('bar', function () {
return 'bar';
}) ->middleware(['auth']); // fortify auth middleware
});
I have some issues with Laravel. I called php artisan route:cache and then the issues began. For example, the Auth routes (login etc.) can be called even if the User is logged in. Then the caching command seems to not clearing the routes. I noticed it because I put my Auth routes in the middleware guests because of the rendering of the Auth routes. After I ran route: clear it worked.
Also, the 404 routing doesn't work since that, because if I call a route that doesn't exist, then the Symfony Framework throws an error:
Symfony\Component\Routing\Exception\ResourceNotFoundException
That's my web.php:
Route::get("/installer","install\InstallController#index");
Route::group(["middleware"=>"guest"],function(){
Auth::routes();
Route::post("login","Auth\Logincontroller#authenticate");
});
Route::group(["middleware" => "auth"], function () {
Route::get("/logout","Auth\LoginController#logOut");
Route::get('/', "dashboard\DashboardController#index");
});
Also, the installer route doesn't work. I will always get redirected to localhost/dashboard (even if I change the route name). My domain for the Laravel is called raptor.debug, so I don't know why it's redirecting to localhost.
Can someone point out what I did wrong or is this a bug?
As Anas Bakro pointed out, the command php artisan route:cache will brick the app, when the folder of the controllers are lowercased.
I am new on laravel, and I am making some test and following a tutorial. The matter is that I don´t know what I did two days ago that I got any defaults controllers (maybe only one... I don´t remember, for example: HomeController). I removed the project and create a new one... but now these defaults controllers don´t exit. And my routes.php on app/http folder is like this:
Route::get('/', function () {
return view('welcome');
});
ONLY THIS!!!
I remember that the routes.php file of the first laravel test project had something like: get("home")... or get("login"), etc...
Do I need to install them via artisan or something?
When installing Laravel, i. e. with this command composer create-project --prefer-dist laravel/laravel blog, there is no app/Http/Controller/HomeController.php generated.
You only get one route in your routes.php. That's it!
What you could do, of course, is:
Create a class HomeController.php in app/Htpp/Controllers - use php artisan make:controller HomeController
Change the route in your routes.php to utilize the new HomeController
Route::get('/', 'HomeController#index');
Another thing you may did in the past was running php artisan make:auth to initialize basic controller ands views to get a scaffold for logins/registers of users.
Maybe authentication? It will create some views and routes.
https://laravel.com/docs/5.2/authentication#authentication-quickstart
The command php artisan make:auth if you are using 5.2.*
no you don't if you want to create a controller just tphp artisan make:controller HomeController and a controller will be created for you :)
I've cached my Laravel 5 routes by doing php artisan route:cache.
This went succesfull for quite a while and when I changed a route is could cache them again and it would all work like it should.
The problem is that I've moved some routes to another route group and it seems that they won't be cached for some reason.
I've tried to cache them again but it didn't work. I've also tried php artisan cache:clear but still not working.
Routes.php changes:
Route::group(['prefix' => 'api', 'middleware' => 'auth'], function () {
Route::get('invites', 'InvitationController#get');
Route::get('invites/check', 'InvitationController#check');
});
Changed to:
Route::group(['prefix' => 'api'], function () {
Route::post('auth', 'Auth\AuthController#authenticate');
Route::get('invites', 'InvitationController#get');
Route::get('invites/check', 'InvitationController#check');
});
As you can see I've moved those invitation routes to the Route group without the Authenticate Middleware. When I cached the routes again, it still does execute the Auth Middleware even when they are moved out of the group..
The right syntaxe to remove cached route in laravel 5.* is
php artisan route:clear
Regards
Try:
php artisan optimize
This will clear all the route cache as well as the file cache.
If you don't want to optimize again with every change.
You can try this;
CACHE_DRIVER=file to change => CACHE_DRIVER=array
After, you should clear to folder "bootstrap/cache"
I hope it works.
I have been working with laravel 4 for some time now and i needed to create an admin area so i decided to use a package to keep things all organized and separated from the rest of the application.
So i created a package with composer as 'vendor/admin'.
then i added those lines as documemented on laravel site
AdminServiceProvider.php
public function boot()
{
$this->package('vendor/admin', 'admin');
include __DIR__.'/../../routes.php';
}
public function register()
{
//
$this->package('vendor/admin');
}
I also created a routes.php file in vedor/admin/ directory to route all admin area in this file.
following i run the 'php artisan dump-autoload' and i finalized with this commend on artisan 'php artisan config:publish vendor/admin'
now i wanna be able use this package for mysite.com/admin route and i want the routes.php file in the package to render the routing for that URI, to do that:
Do i need to modify my app/routes.php?
How can i make vendor/admin/src/routes.php file to do the routing for all mysite.com/admin routes?
Thanks.
No you don't need to edit app/routes.php. As long as it doesn't contain any admin routes that could collide with the ones in your package you can leave it that way.
The routes file in a package can be used like the "normal" app/routes.php. An easy way to deal with admin routes is to have a prefix group:
Route::group(array('prefix' => 'admin'), function(){
// all your admin routes. for example:
Route::get('dashboard', '...');
// will match GET /admin/dashboard
});
Besides that, make sure you're package gets loaded correctly! One part being registering the service provider. Assuming the namespace of your package is Admin you need to add Admin\AdminServiceProvider to the providers array in app/config/app.php. More information