please, could you help me a bit?
I have this route in Laravel5:
Route::get('edit_user/{userId}', [
'as' => 'editUser',
'uses' => 'Auth\UserController#editUser'
]);
But when I try to go onto url .../edit_user/19 it wont match and redirect me into /home url...
Anyway, when I use this:
Route::get('edit_user/{userId}', [
'as' => 'editUser',
function($userId) {
die($userId);
}
]);
It returns 19.
Here is also my function:
public function editUser($userId) {
die($userId);
}
Here is also part of my controller:
<?php namespace App\Http\Controllers\Auth;
use App\Models\User;
use Auth;
use Hash;
use Mail;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class UserController extends Controller {
/**
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
*
* show edit user form
*
* #param $id
*/
public function editUser($userId) {
die($userId);
}
Do you know any idea, where might be problem?
Thank you very much for your opinions.
EDIT:
I find out solution -> need to edit exception to auth in __construct.
This should work with the code provided.
Check the following items:
1. Do you have this code in the __construct of your UsersController?
public function __construct()
{
$this->middleware('auth');
}
If so, you need to be lagged in to edit the user.
2. Is there any route listed before edit_user/{userId} that would override this route.
Try moving it to be the very first route.
3. Is you UserController namespaced properly?
<?php
namespace App\Http\Controllers\Auth;
Related
I'm using laravel 8 (Laravel Framework 8.64.0) to create an API. But when make a call to an endpoint, which i define with Route::apiResource it doesn't return the result from a query, returns nothing.
routes\api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\TodoController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
//Route::get('/todos', [TodoController::class, 'index']);
Route::apiResources([
'todos' => TodoController::class,
]);
Models\Todo.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Todo extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'name',
'is_done',
'due_at',
'completed_at',
];
}
Controllers\TodoController.php
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Todo;
class TodoController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return Todo::all();
}
If I add another route:
Route::get('/todos', [TodoController::class, 'index']);
and I call it, it works.
What could possible be the problem?
You may want to try create TodoResource class extend JsonResource by running
php artisan make:resource TodoResource
then inside Todo Controller index function, replace return Todo::all(); with
return TodoResource::collection(Todo::all());
Solution why not work, remove line from
routes\api.php:
use App\Http\Controllers\Api\TodoController;
I use Laravel 6.2
web.php seems to be okay however, BindingResolutionException Target class [App\Http\Controllers\App\Stack\Http\Controllers\HomeController] does not exist.
I think "App\Http\Controllers" is reductant .
How should I remove this extra junk path?
error happens.
Where should fix it?
web.php
<?php
Route::get('/', function () {
return redirect('/home');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
HomeController.php
<?php
namespace App\Stack\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Stack\Http\Middleware\SetDefaultLayoutForUrls;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware(['auth', SetDefaultLayoutForUrls::class]);
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
the file is located at
{thisProject}/app/Http/Controllers
In web.php you say that the controller is at :
App\Stack\Http\Controllers\HomeController
but in your comment you say that the controller is at
{thisProject}/app/Http/Controllers
Try changing your homecontroller namespace to:
namespace App\Stack\Http\Controllers;
And your route to:
Route::get('/home', 'App\Stack\Http\Controllers\HomeController#index')->name('home');
I am using Laravel5.5 and Module package. I have one student module and want to make this as a default for front-end, so committed code of the laravel's default routes/web.php
Here is my student's routes:
<?php
Route::group(['middleware' => 'web', 'namespace' => 'Modules\Student\Http\Controllers'], function() {
/** Frontend routes which does not require authentication
*
*/
Route::get('/', 'FrontEndController#index')->name('frontend.home');
Route::get('/program-search', 'FrontEndController#programs')->name('student.programs');
Route::get('/univeristy-search', 'FrontEndController#univerities')->name('student.universities');
});
And here is my controller code:
<?php
namespace Modules\Student\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Modules\Admin\Http\Models\ProgramCategory;
use Modules\University\Http\Models\Program;
use Modules\Student\Http\Models\Student;
use Modules\University\Http\Models\University;
class FrontEndController extends Controller
{
/**
* Display a listing of the resource.
* #return Response
*/
public function index()
{
return view('student::index');
}
/**
* Show all programs
*/
public function programs(){
$categories = ProgramCategory::orderBy('catagory_name')
->where('status', '=', 'active');
$programs = Program::orderBy('program_name')
->where([
['status', '=', 'active']
]);
$programs->categories = $categories;
return view('student::program_list')
->withPrograms( $programs );
}
public function univerities()
{
return view('student::university_list');
}
}
only first route '/' is working. when I try to access '/program-search' and '/univeristy-search' it throws an error like "No hint path defined for [sutdent]. (View: /var/www/development/unigatenew/Modules/Student/Resources/views/university_list.blade.php)".
What is the wrong I am doing? can anybody help out this?
The mistake was including the same file name inside view. Renaming file name which was included solved the problem.
I am trying to limit my login attempts but still not working
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
and this is ThrottlesLogin.php
protected function hasTooManyLoginAttempts(Request $request)
{
return $this->limiter()->tooManyAttempts(
$this->throttleKey($request), 3, 2
);
}
and i know in laravel 5.4 the AuthenticatesUsers call by default thethrottlesLogin but still dont limit login attempts.
and thanks
You need to use ThrottlesLogins trait in LoginController
I want to create module with number of controller like
Admin is Module name and have AdminController.
Admin module has another controller CategoryController, ProductController.
Now i wanted to use that controller as part of admin module How can i achive that using Artem-Schander/L5Modular
You have the wrong namespace in your CategoryController.php
It should be namespace App\Modules\Admin\Controllers
and not namespace App\Modules\Admin\Controllers\Category
working example:
routes.php:
Route::group(array('module' => 'Admin', 'namespace' => 'App\Modules\Admin\Controllers'), function() {
Route::resource('admin', 'AdminController');
Route::resource('category', 'CategoryController');
});
AdminController.php:
<?php namespace App\Modules\Admin\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\Admin\Models\Admin;
class AdminController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
die('admin controller');
}
}
CategoryController.php:
<?php namespace App\Modules\Admin\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//use App\Modules\Admin\Models\Admin;
class CategoryController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
die('category controller');
}
}
Here you said, you have a blank page. Check your .env file for the debug option and set it to true. Than you should have a detailed debug output.