I have an adminmodul, where I can edit or delete some user profiles and i can activate or deactivate an user profile.
Everything works perfect. But suddenly my routes doesnt work anymore. Now I got the error 404 - page not found.
I dont know what the problem is, because I dont change anything in the code.
I think my routes doesnt working. Does anyone know why?
I tried to change my routes.
<?php
Route::get('/', 'AdminController#index');
Route::get('/{id}',[
'as' => 'adminmodul.deactivate',
'uses' => 'AdminController#deactivate'
]);
Route::resource('/adminmodul', 'AdminController');
Route::get('/{id}/edit', 'AdminController#edit')->name('adminmodul.edit');
this is my web.php
i have a AdminController.php where I have methods: index, create(dont use this), edit, update, destroy and deactivate
for example:
public function destroy($id)
{
$user = User::find($id);
$angebot = Angebot::where('firma', $id);
$angebot->delete();
$user->delete();
return redirect('/')->with('success', 'Nutzer wurde erfolgreich gelöscht');
}
public function deactivate($id)
{
$user = User::find($id);
if ($user->verified == 1){
$user->verified = 0;
$user->save();
$angebot = Angebot::where('firma', $id);
$angebot->delete();
return redirect('/')->with('success', 'Nutzer wurde erfolgreich deaktiviert');
}
if($user->verified == 0){
$user->verified = 1;
$user->save();
return redirect('/')->with('success', 'Nutzer wurde erfolgreich aktiviert');
}
}
the index page is working. My views are located in resources/views/adminmodul and then, create.blade.php, edit.blade.php, show.blade.php and index.blade.php
if i hover over the Link, it shows me the right link /1/edit but if I click on this link it goes to /edit
i tried this:
public function test()
{
return 'test';
}
Route::get('/test', 'AdminController#test');
but it doesnt work either and shows me 404
Why do I get 404 page not found?
I think because of wildcards you should try this order to avoid conflict
Route::get('/', 'AdminController#index');
Route::get('/{id}/edit', 'AdminController#edit')->name('adminmodul.edit');
Route::get('/{id}',[
'as' => 'adminmodul.deactivate',
'uses' => 'AdminController#deactivate'
]);
Route::resource('/adminmodul', 'AdminController');
Put your edit route above Route::resource
Route::get('/{id}/edit', 'AdminController#edit')->name('adminmodul.edit');
Route::resource('/adminmodul', 'AdminController');
Note that your new methods have to go above the Route::resource
Related
I have route like
Route::get('admin/selfcontacteditdata','SelfcontectController#edit')->name('selfcontectedit');
Route::post('admin/selfcontactupdatedata','SelfcontectController#update')->name('selfcontectupdate');
If i just go to my browser and right admin/selfcontacteditdata it redirect me to
admin/newsshowdata
And my index function is
public function __construct()
{
return $this->middleware('auth');
}
public function index()
{
request()->validate([
'email' => 'required',
'mobileno' => 'required',
'facebook'=>'required',
'google'=>'required',
'map'=>'required',
]);
$data = selfcontect::find(1);
return view('/admin/selfcontectedit',compact('data'));
}
And my middleware is
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
My rest admin routes are working fine.
I had the same problem but I was writing table name wrong and my file was not saved as .blade please check are you also doing the same thing and there is no meaning of validation in edit function your edit function must be like
public function edit()
{
$data = selfcontect::find(1);
return view('/admin/selfcontectedit',compact('data'));
}
and your function name should be edit
You should use Accept key not Content/type
You can't redirect through view, actually your are calling view.
Correct syntax is
return view('view_name',compact('data'));
If you want to redirect to any route you have to call like this
return redirect()->to('admin/selfcontacteditdata');
Redirect to a Route
If in your routes.php file you have a route with a name, you can redirect a user to this particular route, whatever its URL is:
app/Http/routes.php:
get('books', ['as' => 'books_list', 'uses' => 'BooksController#index']);
app/Http/Controllers/SomeController.php
return redirect()->route('books');
This is really useful if in the future you want to change the URL structure – all you would need to change is routes.php (for example, get(‘books’, … to get(‘books_list’, …), and all the redirects would refer to that route and therefore would change automatically.
And you can also use parameters for the routes, if you have any:
app/Http/routes.php:
get('book/{id}', ['as' => 'book_view', 'uses' => 'BooksController#show']);
app/Http/Controllers/SomeController.php
return redirect()->route('book_view', 1);
In case of more parameters – you can use an array:
app/Http/routes.php:
get('book/{category}/{id}', ['as' => 'book_view', 'uses' =>
'BooksController#show']);
app/Http/Controllers/SomeController.php
return redirect()->route('book_view', [513, 1]);
Or you can specify names of the parameters:
return redirect()->route('book_view', ['category'=>513, 'id'=>1]);
I have a Admincontroller. Here I can do a simple CRUD function.
With this route: Route::resource('admin', 'AdminController');
Everything (create, edit destroy) works perfect.
Now I have a new function in this Controller.
Named deactivate. Here i can deactivate users.
this is the function:
public function deactivate($id)
{
$user = User::find($id);
if ($user->verified == 1){
$user->verified = 0;
$user->save();
$angebot = Angebot::where('firma', $id);
$angebot->delete();
return redirect('/admin')->with('success', 'Nutzer wurde erfolgreich deaktiviert');
}
if($user->verified == 0){
$user->verified = 1;
$user->save();
return redirect('/admin')->with('success', 'Nutzer wurde erfolgreich aktiviert');
}
}
for this function i have this route:
Route::get('admin/{id}', 'AdminController#deactivate')->name('admin.deactivate');
in my view it looks like this:
<a href="{{ route('admin.deactivate',$user->id)}}">
if i click on this link it goes to /admin/2 (so it gives me the right id)
but it doesnt redirekt to /admin so i think the route doesnt work because the function should work
does anyone know why?
is it because i do this in my ressource controller?
When you want to add an extra route to your resource route,add it above the resource route..
Route::get('admin/{id}', 'AdminController#deactivate')->name('admin.deactivate');
Route::resource('admin', 'AdminController');
I want to add # symbol to url. Just like this. I have tried this in web.php.
Route::get('/#{user}', 'ProfilesController#show');
It did not work. Then I tried Route::get('/#/{user}', 'ProfilesController#show');
It worked but how can I remove the (slash) '/' between # symbol and user id ?
User model:
public function getRouteKeyName()
{
return 'nick';
}
ProfilesController:
public function show(User $user)
{
return view('profiles.show', [
'profileUser' => $user
]);
}
web.php:
Route::get('/#{user}', 'ProfilesController#show');
The way you have it should work. Check your controller code to make sure you are accepting the variable. The code below is what we have working on our site.
web.php
Route::get('/#{username}', [
'as' => 'profile',
'uses' => 'ProfilesController#show'
]);
ProfilesController.php
public function show($username){
...
}
You are better off refactoring your code.. the # sign is a reserved character as is stated here
Hi, I have been trying to solve this issue for hours how, researched Stack Overflow and many other websites, but no luck.
//-------Route
Route::get('/profile/{firstName}', array(
'as' => 'userProfile',
'uses' => 'ProfileController#user'
));
//--------Controller
class ProfileController extends BaseController {
public function user($firstName) {
$user = User::where('firstName', '=', $firstName);
if($user->count()) {
//First record return from the query
$user = $user->first();
return View::make('profile.user')
->with('user', $user);
}
return "sorry, 404";
//return App::abort(404);
}
}
Finally link:
<li> My Profile </li>
------------------------------
*If I put the username into the URL manually, it works, however when I clicked on the link I get localhost:8000/profile/{firstName} with "sorry, 404"
Thank you for the help!!!
Your route userProfile requires the parameter firstName so you have to pass that in when generating the url:
{{ URL::route('userProfile', Auth::user()->firstName) }}
(How you get the firstName in the view doesn't matter I just used Auth::user since I thought this might be your use case)
I am having issues with using multiple route filters in Laravel 4.2 while using the pattern role:permission. I've attached my code below.
This doesn't work at all. When I change roles, it always give one 403 unauthorized. I want both moderator and administrator to access this route.
Perhaps there's a way to tell Laravel, "if the logged in user is either an administrator OR a moderator, then let them access this route".
Route::get('engage', [
'as' => 'engage_path',
'before' => 'role:administrator',
'before' => 'role:moderator',
'uses' => 'ManagementController#showEngagement'
]);
This is my role filter.
Route::filter('role', function($route, $request, $role)
{
if (Auth::guest() or ! Auth::user()->hasRole($role))
{
return Response::make('Unauthorized', 403);
}
});
I suggest you use some kind of delimiter, like a ; to pass in multiple roles.
'before' => 'role:administrator;moderator'
And change the filter:
Route::filter('role', function($route, $request, $value)
{
if(Auth::check()){
$user = Auth::user();
$roles = explode(';', $value);
foreach($roles as $role){
if($user->hasRole($role)){
return;
}
}
}
return Response::make('Unauthorized', 403);
});