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.
I want the views: listings and showlisting pages, to be displayed for guests (without being logged in).
When using:
$this->middleware('auth';
In the construct function of my ListingsController everything works (for logged in users), but when I exclude index and show methods by using:
$this->middlware('auth')->except('index','show');
I get this error:
BadMethodCallException Method
App\Http\Controllers\ListingsController::middlware does not exist.
I have searched for a few days, and I haven't found any solution.
ListingsController.php
public function __construct()
{
$this->middlware('auth')->except('index', 'show');
}
web.php (route file)
Route::get('/', 'ListingsController#index');
Route::resource('listings', 'ListingsController');
Route::get('/dashboard', 'DashboardController#index');
Auth::routes();
You have:
$this->middlware('auth')->except('index', 'show');
Middleware is misspelled and your error reflects that. It should be:
$this->middleware('auth')->except(['index', 'show']);
When I clear caches in my Laravel 5.2 project, I see this error message:
[LogicException]
Unable to prepare route [panel] for serialization. Uses Closure.
I think that it's related with a route
Route::get('/article/{slug}', 'Front#slug');
associated with a particular method in my controller:
public function slug($slug) {
$article = Article::where('slug',$slug)->first();
$id = $article ->id_article ;
if ( ($article=== null) || (is_null($id)) ) return view('errors/Db');
else return view('detail')->with(array('article'=> $article, 'title'=>'My title - '.$article->title));
}`
In short, from a master view I pass $slug, that is a shortlink to the article, with $slug , which is unique in the database, I identify the record and then I pass it's contents to the detail view.
I didn't have any problem when I wrote the method, infact it worked like a charm, but after I cleaned caches, I get that error and the links in the master view don't show any shortcode.
Where am I doing wrong?
I think that it's related with a route
Route::get('/article/{slug}', 'Front#slug');
associated with a particular method in my controller:
No, thats not it. The error message is coming from the route:cache command, not sure why clearing the cache calls this automatically.
The problem is a route which uses a Closure instead of a controller, which looks something like this:
// Thats the Closure
// v
Route::get('/some/route', function() {
return 'Hello World';
});
Since Closures can not be serialized, you can not cache your routes when you have routes which use closures.
If none of your routes contain closures, but you are still getting this error, please check
routes/api.php
Laravel has a default auth api route in the above file.
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which can be commented or replaced with a call to controller method if required.
This is definitely a bug.Laravel offers predefined code in routes/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which is unabled to be processed by:
php artisan route:cache
This definitely should be fixed by Laravel team.(check the link),
simply if you want to fix it you should replace routes\api.php code with some thing like :
Route::middleware('auth:api')->get('/user', 'UserController#AuthRouteAPI');
and in UserController put this method:
public function AuthRouteAPI(Request $request){
return $request->user();
}
The Actual solution of this problem is changing first line in web.php
Just replace Welcome route with following route
Route::view('/', 'welcome');
If still getting same error than you probab
the solustion when we use routes like this:
Route::get('/', function () {
return view('welcome');
});
laravel call them Closure so you cant optimize routes uses as Closures you must route to controller to use php artisan optimize
Check your routes/web.php and routes/api.php
Laravel comes with default route closure in routes/web.php:
Route::get('/', function () {
return view('welcome');
});
and routes/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
if you remove that then try again to clear route cache.
If you're coming to this problem because you've upgraded Laravel <5.8 project up to >=5.8, you've likely used the ./vendor/bin/carbon-upgrade method to upgrade the project as suggested by your terminal. In this case, you simply need to remove the following two blocks from the bottom of your composer.json file and composer install again:
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
],
If someone is still looking for an answer, for me the problem was in routes/web.php file. Example:
Route::get('/', function () {
return view('welcome');
});
It is also Route, so yeah...Just remove it if not needed and you are good to go!
You should also follow answers provided from above.
In order to troubleshoot this (at least in laravel 6):
The action property inside Route.php has all the info needed. A better error message should be possible to provide by laravel.
What I did was to add a dd($this->action) just before the exception is thrown here:
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Routing/Route.php#L917
With that in place I could easily pinpoint the location, in my case api.php and lines 22-24:
array:6 [
"middleware" => "api"
"domain" => "local-api.mydomain.com"
"uses" => Closure()^ {#6497
class: "App\Providers\RouteServiceProvider"
this: App\Providers\RouteServiceProvider {#5743 …}
file: "./routes/api.php"
line: "22 to 24"
}
"namespace" => "App\Http\Controllers"
"prefix" => null
"where" => []
]
This is how I solved mine.
Navigate to routes directory
Then open api.php
comment code that looks like this:
Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });
Then run
php artisan optimize
check that your web.php file has this extension
use Illuminate\Support\Facades\Route;
my problem gone fixed by this way.
So I have a route profile/{user_id}
How do I redirect user to that URL when they click on link?
Here's my controller:
{
function checkid($user_id) {
if (Auth::check())
{
$user_id = Auth::id();
return view('profile', [
'id' => $user_id
]);
}
}
}
Bit confused with the question but Laravel uses ID as default for dependency injection and changing it is easy: just change the routeKey in the model BUT in your instance, you're using the signed in user. So forgot the id!
<?php
namespace App\Http\Controllers;
class RandomController extends Controller {
public function index()
{
return view('profile');//use auth facade in blade.
}
}
In your routes use a middleware to prevent none authenticated users from reaching this method
<?php
Route::group('/loggedin', [
'middleware' => 'auth',
], function() {
Route::get('/profile', 'RandomController#index')->name('profile.index');
});
Now to redirect in your blade file use the route function, don't forget to clear your cache if you've cached your routes!
<h1>Hi! {{Auth::user()->name}}</h1>
View profile
Because I used the name method on the Route I can pass that route name into the route function. Using php artisan route:list will list all your route parameters which is cool because it will also tell you the names and middlewares etc.
if I had a route which required a parameter; the route function accepts an array of params as the second parameter. route('profile.index', ['I_am_a_fake_param' => $user->id,]).
Let me know if you need help with anything else.
You can redirect with the redirect() helper method like this:
return redirect()->url('/profile/' . $user_id);
But I'm not really following your usecase? Why do you want to redirect? Do you always want the user to go to their own profile? Because right now you are using the id from the authenticated user, so the user_id parameter is pretty much useless.
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)