Laravel route not calling function of controller - laravel

I have a form in a page (blade) which redirects to a route which is to call a function in a controller however it does not even go inside the function because even a simple dd(); cannot be executed. when in route, if I change to
Route::post('edit/profile', function(Request $request){
dd($request);
});
it works. I tried to change both route's function name and controller's function name to another name still doesn't recognize.
My current route
Route::post('edit/profile', 'Auth\LoginController#updateUser');
My form line
<form action="{{url('/edit/profile')}}" method="post">
{{ csrf_field() }}
My function inside LoginController
public function updateUser(Request $request)
{
//insert code here
}

I know it's late, but did you solve the problem? I've had a simmilar issue, inside auth group a route was not working. In my case, I had to put the route after the "Route::resource". So make sure if your are making a route that has a resource tagged, add the other routes before the Route::resource. For example:
Route::group(['middleware' => ['auth']], function() {
Route::resource('category', 'CategoryController');
Route::get('category/order', 'CategoryController#order')->name('categoryOrder');
});
This won't work. Just put the 'category/order' route before the resource route like this:
Route::group(['middleware' => ['auth']], function() {
Route::get('category/order', 'CategoryController#order')->name('categoryOrder');
Route::resource('category', 'CategoryController');
});
This should work. Hope this helps someone.

Related

Routing when using api controller

i am really frustrated and dont know why i cannot route to my specified page like
profile
i am using an api controller and this is my controller and i am trying just to return a view
public function profile()
{
return view('layouts.main');
}
as simple as thats it just to return a view , all it does is that it reloads my welcome page
please i need your assistance
here is my api controller
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResources(['user' => 'API\UserController']);
Route::get('profile', 'API\UserController#profile')->name('profile');
Route::put('profile', 'API\UserController#updateProfile');
Route::get('findUser', 'API\UserController#search');
Route::apiResources(['service' => 'API\ServiceController']);
If you put this in your api.php
for laravel 8
Route::resource("/users",UserController::class);
Than the routes will be automatically prefixed with /api. So the routes will look like this:
/api/users
/api/users/{user}
...
So you just have to suppose that the api prefix, is automatically added from routes/api.php.
but if you put it in your web.php
Route::resource("/users",UserController::class);
the routes will look like this:
{{ route('users.index') }}
{{ route('users.profile') }}
...
for create an action like profile you have to make that:
Route::resourse('profile',[UserController::class,'profile')->name('profile');
For more information about your routes, you can run php artisan route:list and you can check how do your routes look like.

Laravel default Auth::routes() inside of group prefix

I'm attempting to create a prefix with a variable for "companies" to login to the platform. All users are tied to a company so the normal /login isn't desired. I'd like to use something like `/acme-company-name/login
I am using the default Laravel auth via: php artisan make:auth on a fresh install.
Route::group(['prefix' => '{company}'], function () {
Auth::routes();
});
When I try navigating to /company-name/login I see the following error:
Missing required parameters for [Route: login] [URI: {company}/login].
Looking inside the auto-generated login.blade.php I see the function call route('login') and this seems to be where everything is breaking. I think I need some way to supply a variable to that function or redefine what the "login" route is in some fashion ? I'd rather not have to replace the call Auth::routes() but will certainly do so if that is required to fix this issue.
I should note, i've tried defining the group 'as' => 'company' and changing route('company.login') but then I am told the route company.login is not defined.
Can you try by passing $company variable to the function as well?
Route::group(['prefix' => '{company}'], function ($company) {
Auth::routes();
});
And make sure you pass the company-name when calling the route as it's a required parameter.
In login.blade.php use {{ url("$company/login") }} instead of route('login').
route() helper has more than one parameter ;)
route('login', ['company' => $company])
you need to share your prefix in your views and set route as the following:
Route::group(['prefix' => '{company}'], function ($company) {
view()->share('company', $company); // share $company in views
Auth::routes();
});
now you have $company which is instance of Router and you need access to route prefix value for this you need get current route and get company parameter so you should rewrite your route() helper function as the following:
{{ route('login',['company'=>$company->getCurrentRoute()->__get('company')]) }}
// getCurrentRoute Get the currently dispatched route instance
//__get Dynamically access route parameters
EDIT:
you can write custom helper function as the following:
/**
* Generate the URL to a named route for Company Auth.
*
* #param string $name
* #param Router $company
* #return string
*/
function companyRoute($name,$company)
{
return app('url')->route($name, ['company'=>$company->getCurrentRoute()->__get('company')] ,true);
}
Please check the code that is working fine for me.
Route::group(['prefix' => '/{company}'], function () {
// ensure that auth controllers exists in right place (here it is App\Http\Controllers\Auth\LoginController)
// in LoginController funtion
Auth::routes();
//or you can try using custom routing like this
Route::get('{something}', function($company, $something)
{
var_dump($company, $something);
});
});
If you define this route at the end of the file then the error that you have mentioned we face.
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: login] [URI: {company}/login]. (View: \resources\views\auth\login.blade.php)
Try with defining this route as first route in web.php and check.
Thanks
Looking inside the auto-generated login.blade.php I see the function call route('login') and this seems to be where everything is breaking.
Yes. By doing
Route::group(['prefix' => '{company}'], function () {
Auth::routes();
});
you are making all auth routes take a company parameter. So, in your views, instead of route('login'), you can do route('login', ['company' => Request::route('company')]). (And the same for all auth routes).
Then probably in your App\Http\Controllers\Auth\LoginController you will need to override the login method accordingly:
public function login(Request $request, $company)
{
// you can copy some behaviour from
// https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php#L28
}

Laravel redirect route with a parameter from controller

I'm trying to redirect a route from a controller function after a form submit process in Laravel 5.4 as it is said in link below
https://laravel.com/docs/5.4/redirects#redirecting-named-routes
Route;
Route::group(['middleware' => ['api'], 'prefix' => 'api'], function () {
Route::post('doSomething', 'Page#doSomething');
});
Route::post('/profile', function () {
//..
});
Controller;
public function doSomething(Request $request){
return redirect()->route('profile', ['id'=>1]);
}
When I try to redirect I get this error.
InvalidArgumentException in UrlGenerator.php line 304: Route [profile]
not defined.
I have searched several times about redirection but I got similar results.
Any suggestions ? Thank you.
route() works with named routes, so name your route
Route::post('/profile', function () {
//..
})->name('profile');

Protect routes with middleware Laravel

I have implemented middleware roles and permissions control in my app, but I cannot figure out why it only allows me to define one '/' route. The second one is still pointing to '/home' even though I override AuthController redirectTo variable.
My routes:
Route::group(['middleware' => 'role:user'], function()
{
Route::get('/', 'ScoresController#user');
});
Route::group(['middleware' => 'role:admin'], function()
{
Route::get('/', 'PagesController#home');
});
In any case after authentication user with user role redirecting to '/home'.
Like Simon says your second route will override the first one what you could do is load another controller wich redirects you to another page via redirect()
or write it as a route itself.
Could look like this:
Route::get('/', function(){
$user = Auth::user();
if($user->is('admin') {
return redirect('admin');
}else{
return redirect('user');
}
});
Route::get('admin', ['middleware' => 'role:admin', 'uses'=> 'PagesController#home']);
This is just one of many possibilities hope it helps you.

How admin route in laravel 4

My folder structure is following:
Controller>admin>`loginController`
view>admin
model>admin
In LoginController includes authorization process
I have used routes:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'LoginController');
});
But i also found error 'Controller method not found.
You either have to use Route::controller() or specify the action that you want to route to. For example:
Controller
public function showLogin(){
return View::make('login');
}
Route
Route::get('/', 'LoginController#showLogin');
(With the # part you tell Laravel what controller method you want to call)

Resources