Laravel Routing resource controller - php

I am using laravel rource controller and routes and having problem in view file while using Form::Open method
Following is my code in routes.php
$router->group(['namespace' => 'Admin', 'middleware' => 'auth'],
function (){ resource('admin/post', 'PostController',
['only' => ['index', 'create', 'store', 'newe', 'afadfafafa']]);
resource('admin/tag', 'TagController');
get('admin/upload', 'UploadController#index');
});
In view.php
{!! Form::open(array('action' => 'Admin\PostController#store')) !!}
In Controllers/Admin/AdminController.php I do have a method named store.
Still I am getting, form action url renders as "http://localhost/laravel/admin/post" i.e. to index action and not store action.
What is problem in my code.

i try your code and it works right
Admin\PostController.php
public function index()
{
echo "method index";
}
public function store(Request $request)
{
echo "method store";
}

This is what i am expecting with current code should work

Related

Laravel $id return 'en' instead of id

My route:
Route::group([
'prefix' => '{locale}',
'where' => ['locale' => '[a-zA-Z]{2}'],
'middleware' => 'setlocale'],
function () {
// GROUP FOR AUTHENTICATION
Route::group([
'middleware' => 'auth:sanctum', 'verified',
], function () {
// GROUP FOR ADMIN
Route::group([
'prefix' => 'admin',
'as' => 'admin.',
], function () {
Route::resource('partner', PartnerFormController::class);
});
});
});
My view that when I click, it shows the details page:
Edit
My controller:
public function show(PartnerForm $partnerForm, $id)
{
$details = DB::table('partner_forms')->where('id', $id)
->first();
return view('admin.partner-details', compact('details'));
}
I tried to call $details->name or $details->in my view details page but it didnt work. The URL is working properly by displaying http://127.0.0.1:8000/en/admin/partner/1 but when i dd() my $id in controller it returns en, which I believe is the en from the URL.
Instead of sending the ID along with the route, send all the variables and enter the information into the new page in the control without the need to connect to the database and using route model binding.
Example :
view A
Foo
route
Route::get('.../{partner}', [Controller::class, 'Bar'])->name('admin.partner.show');
controller
public function Bar(Partner_MODEL $partner){
return view('view_name', compact('partner'));
}
view B : use $partner.
Important note : in (route, controller method, compact meethod) The variable name must be similar

Conditional routing based on Middleware

I need to call different controller for the same url based on a middleware. The url has to be the same, so redirecting in the middleware is not an option. The code below is sample, controllers for dozens for routes are already finished, so checking the session value there is not an option either.
Tried to create two different middleware (has/hasnt the session value), but the latter route group overwrites the previous anyway. Any clue? Maybe a different approach needed?
route.php looks like this:
Route::group(array('namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'auth'), function () {
// set of default routes
Route::get('/', array('as' => 'admin', 'uses' => 'FirstController#index'))->middleware('admin');
Route::get('/profile', array('as' => 'profile', 'uses' => 'FirstController#profile'))->middleware('admin');
Route::group(array('middleware' => 'sessionhassomething'), function () {
// set of the same routes like above but overwritten if middleware validates
Route::get('/', array('as' => 'admin', 'uses' => 'SecondController#index'))->middleware('admin');
Route::get('/profile', array('as' => 'profile', 'uses' => 'SecondController#profile'))->middleware('admin');
});
});
SessionHasSomething middleware:
class sessionHasSomething {
public function handle($request, Closure $next)
{
if(session()->has("something_i_need_to_be_set")) {
return $next($request);
}
// return what if not set, or ...?
}
}
Thanks in advance!
If you are only checking if session()->has('something'), it is possible to use route closures to add a condition within the route which needs to be dynamic.
Below is an example:
Route::get('/', function() {
$controller = session()->has('something')) ? 'SecondController' : 'FirstController';
app('app\Http\Controllers\' . $controller)->index();
});
->index() being the method within the controller class.
We almost have the same issue and here's what I did. (I didn't use middleware).
In my blade.php, I used #if, #else and #endif
<?php
use App\Models\User;
$check = User::all()->count();
?>
#if ($check == '0')
// my html/php codes for admin
#else
// my html/php codes for users
#endif
you can also do that in your controller, use if,else.

L5.3 - Call route from another route

I've a simple route into the file web.php:
Route::get('first/{param?}', [
'uses' => 'App\Http\Controllers\MyController#index',
'as' => 'myControllerIndex'
]);
Now, I'd like to create a second route that uses the first route but passing specific params.
I tried something like this:
Route::get('second', function () {
return file_get_contents(route('myControllerIndex', ['param' => 'book1']));
});
but it doesn't work.
Can anyone help me?
Thank you.
You could use a redirect
Route::get('second', function () {
return redirect()->route('myControllerIndex', ['param' => 'book1']);
});
Or you can access the controller directly
Route::get('second', function () {
return app('App\Http\Controllers\MyController')->index('book1');
});

NotFoundHttpException - Laravel

i want to make edit-update function..
this is my code :
Admin Controller
public function edit_ist($id_prog)
{
$program_studi = ProgramStudi::find($id_prog);
return view('edit_ist_program_studi',compact('program_studi'));
}
public function update_ist($id_prog)
{
$istUpdate = Request::all();
$program_studi = ProgramStudi::find($id_prog);
$program_studi->update($istUpdate);
return redirect('administrator');
}
Form open in view edit_ist_program_studi
{{ Form::model($program_studi,['method'=>'PATCH','route'=>['update_prodi',$program_studi->id_prog]])}}
Routes:
Route::patch('admin_page/edit_prodi/{id_prog}',
['as' => 'update_prodi', 'uses' => 'AdminController#update_ist']);
But i found error NotFoundHttpException, can you help me to fix this ? thank you
You are missing the GET route to the edit page.
Add something like this:
Route::get('admin_page/edit_prodi/{id_prog}', ['as' => 'edit_prodi', 'uses' => 'AdminController#edit_ist']);

i cant call a function on the address bar without including the index file

i am new to laravel and using laravel 5.so i added a
Route::get('/', 'WelcomeController#index');
Route::get('contact', 'WelcomeController#contact'); //this is wat i added
Route::get('home', 'HomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
to the routes.php file then created a public function contact to echo 'contact me' upon being called in the WelcomeController.php file .
public function contact()
{
return 'Contact me!';
}
}
the problem comes when i call the 'contact' function on the address bar all i get is 404 not found.help

Categories