dev deploy complaining about closures - php

I ran deb deploy and it got down to this point.
The command "/usr/bin/php ~/NetTube/releases/1/artisan optimize" failed.
Exit Code: 1 (General error)
Host Name: 68.183.20.108
================
Configuration cache cleared!
Configuration cached successfully!
Route cache cleared!
In Route.php line 917:
Unable to prepare route [api/user] for serialization. Uses Closure.
I've looked into this and it seems that I can't use closures with routes, which is fine. I'm just not sure how to prevent closures.
This is my web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
Route::get('/','HomeController#index')->name('home');
Auth::routes();
// Route::get('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => ['auth']], function(){
Route::get('/upload','VideoUploadController#index');
Route::get('/channel/{channel}/edit','ChannelSettingsController#edit');
Route::put('/channel/{channel}/edit','ChannelSettingsController#update');
});
Route::get('/channel/{channel}','ChannelController#index');
Route::get('/{channel}','ChannelController#index');
I have tried putting the routes not in a group with
Route::get('/upload', [
'uses' => 'VideoUploadController#index',
'middleware' => ['auth'],
]);
Route::get('/channel/{channel}/edit', [
'uses' => 'ChannelSettingsController#edit',
'middleware' => ['auth'],
]);
Route::put('/channel/{channel}/edit', [
'uses' => 'ChannelSettingsController#update',
'middleware' => ['auth'],
]);
I am running laravel 6.17.1. I hope I gave enough information to anybody that can help.

the problem resides on the routes\api.php.
Do something like Route::middleware('auth:api')->get('/user', 'MyProfileController#index');. This route needs to be binded to a controller. If you don't need the route, comment or delete it.

Related

Remove Laravel Package routes and add new

I want to remove Laravel Package routes and add new ones.
I installed this package https://github.com/jeremykenedy/laravel-roles on my Laravel 6.X application, I wanted to disable the routes so I would like to add a prefix of admin/ before the set routes...
Changing the routes in vendor folder doesn't help because it will get updated and gone if you run composer update, any idea on how to solve this issue, other than forking the repo?
I didn't have time to actually scan through all of the environment variables that you can configure in the package. I would recommend to check if there are any config values that you can override first. However, if you can't, then i am sure you could copy the routes and put it in your web.php
<?php
/*
|--------------------------------------------------------------------------
| Laravel Roles And Permissions Web Routes
|--------------------------------------------------------------------------
|
*/
Route::group([
'middleware' => ['web'],
'prefix' => 'admin',
'as' => 'laravelroles::',
'namespace' => 'jeremykenedy\LaravelRoles\App\Http\Controllers',
], function () {
// Dashboards and CRUD Routes
Route::resource('roles', 'LaravelRolesController');
Route::resource('permissions', 'LaravelPermissionsController');
// Deleted Roles Dashboard and CRUD Routes
Route::get('roles-deleted', 'LaravelRolesDeletedController#index')->name('roles-deleted');
Route::get('role-deleted/{id}', 'LaravelRolesDeletedController#show')->name('role-show-deleted');
Route::put('role-restore/{id}', 'LaravelRolesDeletedController#restoreRole')->name('role-restore');
Route::post('roles-deleted-restore-all', 'LaravelRolesDeletedController#restoreAllDeletedRoles')->name('roles-deleted-restore-all');
Route::delete('roles-deleted-destroy-all', 'LaravelRolesDeletedController#destroyAllDeletedRoles')->name('destroy-all-deleted-roles');
Route::delete('role-destroy/{id}', 'LaravelRolesDeletedController#destroy')->name('role-item-destroy');
// Deleted Permissions Dashboard and CRUD Routes
Route::get('permissions-deleted', 'LaravelpermissionsDeletedController#index')->name('permissions-deleted');
Route::get('permission-deleted/{id}', 'LaravelpermissionsDeletedController#show')->name('permission-show-deleted');
Route::put('permission-restore/{id}', 'LaravelpermissionsDeletedController#restorePermission')->name('permission-restore');
Route::post('permissions-deleted-restore-all', 'LaravelpermissionsDeletedController#restoreAllDeletedPermissions')->name('permissions-deleted-restore-all');
Route::delete('permissions-deleted-destroy-all', 'LaravelpermissionsDeletedController#destroyAllDeletedPermissions')->name('destroy-all-deleted-permissions');
Route::delete('permission-destroy/{id}', 'LaravelpermissionsDeletedController#destroy')->name('permission-item-destroy');
});
If you want to keep your web.php clean, then create a file at routes/laravel-permissions.php and then in your RouteServiceProvider load the routes as follow,
protected function mapLaravelPermissionRoutes()
{
Route::prefix('admin')
->as('laravelroles::')
->middleware('web')
->namespace('/jeremykenedy\LaravelRoles\App\Http\Controllers')
->group(base_path('routes/laravel-permissions.php'));
}
If you do load it this way, then make sure that you remove the grouping from your laravel-permissions.php i.e. remove the following code
Route::group([
'middleware' => ['web'],
'prefix' => 'admin',
'as' => 'laravelroles::',
'namespace' => 'jeremykenedy\LaravelRoles\App\Http\Controllers',
], function () {
Now you just need to map the method as follow
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapLaravelPermissionRoutes();
}

Difference between 'web' and 'auth' middleware?

I have faced some problem when i am going to use middleware group of Laravel 5.2 framework.
My routes.php file is:
Route::group(['prefix' => 'categories'], function () {
Route::get('all', ['as' => 'allCategory' , 'uses' => 'CategoryController#index']);
Route::get('add', ['as' => 'addCategory', 'uses' => 'CategoryController#create']);
Route::get('edit/{id}', ['as' => 'editCategory', 'uses' => 'CategoryController#edit']);
Route::post('save', ['as' => 'saveCategory', 'uses' => 'CategoryController#store']);
Route::put('update', ['as' => 'updateCategory', 'uses' => 'CategoryController#update']);
Route::get('delete/{id}', ['as' => 'deleteCategory', 'uses' => 'CategoryController#destroy']);
});
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
});
I am using here laravel defaults login/registration authentication.Using php artisan make:auth command.I want to give user restricted for some routes such as categories route group.So,
How can i restrict a user for categories route group?
If i use Route::group(['middleware' => ['auth']], function () { }); then i got an error. So what is the difference between 'web'
and 'auth' middleware ?
Thanks.
N.B : If you need to know about any files then just comment me below i will add those files.
This is a feature of laravel 5.2. 2 default middleware is web and api.
You need place category group route inside web middleware.
Web middleware make your request contains cookies, session, csrf_token used for authentication. Otherwise, api middleware used for application that simple query get or post without request header, assume mobile app.
Auth middleware based on web middleware.

Laravel 5.2.29 session::flash lost instantly

I created a new laravel project and laravel flash seems to not be working as i want. The moment I return to a route the flash is gone. I have controller method that does absolutly nothing but flash and return to a route.
Like so
public function activateContract(Request $request ){
return redirect()->to('test')->with('status', 'test');
}
My routes file
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function(){
return redirect()->intended(route('contract.index'));
});
Route::group(['middleware' => 'auth'], function () {
Route::group(['prefix' => 'contract'], function(){
Route::get('', ['as' => 'contract.index' , 'uses' => 'User\ContractController#index']);
Route::post('', ['as' => 'contract.index' , 'uses' => 'User\ContractController#activateContract']);
Route::get('mijn', ['as' => 'user.contract.index' , 'uses' => 'User\ContractController#userContracts']);
});
});
Route::get('test', function(){
dd(session('status'));
});
c});
Here is the ouput of the die dump in the test route witch magicaly lost the flash message.
null
You need to send the message with the redirect like so:
return redirect()->to('test')->with('status', 'test');
And then you can access it with the session helper function:
session('status');
docs
Edit: Place your Route::get('test') into your group with middleware 'web'.
See more about HTTP Middleware here
Laravel 5.2 changed middleware document as :Keep in mind, the web middleware group is automatically applied to your default routes.php file by the RouteServiceProvider.
It means that if you keep a route in another ['middleware' => 'web'], the data set by session()->flash() would be lost by executed web middleware twice.
It is also the answer someone asked why web executed twice.
Laravel 5.2 : Web middleware is applied twice

Laravel post route controller method not getting called

I'm building an app in Laravel and I already had my user registration working fine, I now want to modify how the registration works so I modified my route to call a different method.
Originally the route was defined like so:
Route::post('register', 'Auth\AuthController#postRegister');
And this worked fine. I then changed it to:
Route::post('register', 'Auth\AuthController#someOtherMethod');
This method is defined as follows:
public function someOtherMethod(Request $request)
{
die('If the method is called you should see this');
}
However, it doesn't get called (the message doesn't show). Instead it redirects me to the root of the site.
Note that I have a cache-busting script on my server that I run every time I have weird issues like this which runs the following commands:
php artisan route:clear
php artisan cache:clear
service php5-fpm restart
service nginx restart
I also run the page in an incognito/private window every time I make a change.
Now for the interesting part; I tried undoing the changes I made so that it calls postRegister again, I fully expected this to make it revert to the default behaviour but it still redirects me to the root of the site! So now I don't even have a registration page that functions at all.
Does anyone have any idea what's going on?
Thanks in advance for your help.
Edit:
Here's my full routes.php:
use Illuminate\Http\Request;
Route::group(['middleware' => 'web'], function () {
/** Public routes **/
Route::get('', 'HomepageController#index');
Route::get('/', 'HomepageController#index');
Route::get('terms', function() {
return view('terms');
});
Route::get('privacy', function() {
return view('privacy');
});
/** Public auth routes **/
Route::get('register', 'RegistrationController#index');
Route::post('register', 'Auth\AuthController#postRegister');
Route::get('login', function() {
return view('auth.login');
});
Route::post('login', 'Auth\AuthController#postLogin');
Route::get('logout', 'Auth\AuthController#getLogout');
Route::get('dashboard/login', function() {
return view('admin.login');
});
Route::post('dashboard/login', 'AdminAuth\AuthController#postLogin');
Route::get('dashboard/logout', 'AdminAuth\AuthController#getLogout');
/** Admin routes **/
Route::get('dashboard', [
'middleware' => 'admin',
'uses' => 'Admin\DashboardController#index'
]);
Route::get('dashboard/users', [
'middleware' => 'admin',
'uses' => 'Admin\DashboardController#showUsers'
]);
Route::get('dashboard/search', [
'middleware' => 'admin',
'as' => 'adminSearch',
'uses' => 'Admin\DashboardController#query'
]);
/** Admin auth routes **/
Route::get('dashboard/staff/create', [
'middleware' => 'admin',
function () {
return view('admin.register');
}
]);
Route::post('dashboard/staff/create', [
'middleware' => 'admin',
'uses' => 'AdminAuth\AuthController#postRegister'
]);
/** Controllers **/
Route::controllers([
'password' => 'Auth\PasswordController',
]);
});
make sure that you config/auth.php is calling proper Auth guard. Also redirectsTo is by default set to '/' which is route of site. What is in your middleware? the default RedirectIfAuthenticated middleware has by default entry to point to root of the site. Only these 3 scenarios possibly acting other than expected.

Laravel 5.2 Auth not Working

As you guys know Laravel 5.2 was released a few days ago. I am trying this new version. I made a new project using the following command on CLI:
laravel new testapp
As per documentation of Authentication Quickstart, I followed the following command to scaffold routes and views of authentication:
php artisan make:auth
It worked fine. Registration is working fine. But I am facing problem in Login. After login I tested following in route.php file:
Route::get('/', function () {
dd( Auth::user());
return view('welcome');
});
Auth::user() is returning null and also Auth::check() and Auth::guest() are not working appropriately. I have tried same thing again and again two three times by making new projects but couldn't get the correct results.
Below is the complete route.php
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
dd( Auth::());
return view('welcome');
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
});
Can anyone help me? or Is anyone facing the same problem? How can I fix it?
Laravel 5.2 introduces the middleware groups concept: you can specify that one or more middleware belongs to a group, and you can apply a middleware group to one or more routes
By default Laravel 5.2 defines a group named web, used to group the middleware handling session and other http utilities:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
So, if you want session handling, you should use this middleware group for all the routes in which you want to use authentication:
Route::group( [ 'middleware' => ['web'] ], function ()
{
//this route will use the middleware of the 'web' group, so session and auth will work here
Route::get('/', function () {
dd( Auth::user() );
});
});
UPDATE FOR LARAVEL VERSION >= 5.2.27
As of Laravel 5.2.27 version, all the routes defined in routes.php are using by default the web middleware group. That is achieved in app/Providers/RouteServiceProvider.php :
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web'
], function ($router) {
require app_path('Http/routes.php');
});
}
So you don't need anymore to add manually the web middleware group to your routes.
Anyhow, if you want to use the default authentication for a route, you still need bind the auth middleware to the route

Categories