I have a route as below
Route.php
Route::get('u/list/{type}', 'UsersController#listAll');
UsersController.php
class UsersController extends BaseController
{
public function listAll($id)
{
$users = User::all();
return Response::json( $users );
}
}
this works well, i can pass the type into the listAll() method
Now i have another url which is like this
Route::get('u/doctor', 'UsersController#listAll');
Route::get('u/receptionist', 'UsersController#listAll');
This is equivalent to
Route::get('u/list/1', 'UsersController#listAll');
Route::get('u/list/2', 'UsersController#listAll');
is there a way for the URL "u/doctor" to pass the number 1 to listall() method
Or else i will have to rewrite the function with a new name.
Use a closure instead of a string.
Route::get('u/doctor', function(){
return (new UsersController)->listAll(1);
});
Related
How do I pass a true false value to the controller from the redirect in the class and to the router and back to another function in the same controller class if that makes sense
Like
public function 1() {
return redirect('route2');
}
public function2() {
I need to access the variable here that some how gets passed from the first function
}
Because these functions are both on my main controller and I need to pass a variable through the route
and back into the controller or is there a way to put a state variable on the class or something I just need to call a function on the controller with conditions from the previous controller function that called called the redirect route.
Also sorry if I am mixing up class and function I am new to laravel and MVC in general.
You can do something like this:
public function first() {
return redirect()->action(
[YourController::class, 'second'], ['value' => true]
);
}
public function second($value = null) {
// whatever you want
}
https://laravel.com/docs/9.x/redirects#redirecting-controller-actions
I think this code help you:
public function 1() {
return to_route('YOUR_ROUTE_NAME', ['value' => 'some things...']);
}
public function2(Request $request, $value) {
// Use the value passed as a route parameter
// $value is 'some things...'
}
I am stuck in resource routing
when I enter url netbilling.test/customer it goes to customer index file but when I enter url netbilling.test/customer/index nothing is returned. Also guide me if I have to route different method than in resource what is the method for that.
here is my web.php,
Route::get('/dashboard', function () {
return view('dashboard/index');
});
Route::resource('/customer','CustomerController');
here is my customer controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use App\Package;
use Redirect,Response;
class CustomerController extends Controller
{
public function index()
{
$packages = Package::get();
$customers = Customer::orderBy('id', 'DESC')->get();
return view('customer/index', compact('customers','packages'));
}
public function create()
{
//
}
public function store(Request $request)
{
//
}
public function show($id)
{
//
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
}
}
Without custom route specification, this is how the index route maps to a Resource Controller, taken from Actions Handled By Resource Controller:
Verb
URI
Action
Route Name
GET
/photos
index
photos.index
So if you want URI /customer/index to work, then you need to specify this explicitly in your Controller:
use App\Http\Controllers\CustomerController;
Route::resource('customer', CustomerController::class);
Route::get('customer/index', [CustomerController::class, 'index'])->name(customer.index);
OrderController.php
if (request('payment_method') == 'online') {
return redirect(route('payments.pay', $order->id));
}
web.php
Route::POST('/pay/{orderId}', 'PublicSslCommerzPaymentController#index')->name('payments.pay');
PublicSslCommerzPaymentController.php
session_start();
class PublicSslCommerzPaymentController extends Controller
{
public function index(Request $request, $ordId)
{
//code...
}
}
Here in index function I need the order id from `OrderController.
But the Error I am getting
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
if you want to redirect to named route you can use this:
return redirect()->route('payments.pay', ['orderId' => $order->id]);
if you want generate redirect to controller action you can try this:
return redirect()->action(
'PublicSslCommerzPaymentController#index', ['ordId' => $order->id]]
);
Just change in your web.php from POST method to GET method
Something like this
Route::GET('/pay/{orderId}', 'PublicSslCommerzPaymentController#index')->name('payments.pay');
I am new in laravel,
How to passed request data into controller ? Just like happen on view ?
Route::get('/kelihatan', function (Request $request) {
return view( 'pages' , [ 'page' => 'index' , '_request' => $request->all() ] );
});
How to passing request data to controller before passed into view or model ? Just like this ??
Route::get( '/{page}', 'UserController#show' );
Mention the path in route first depending upon the type of request i.e. Get or Post
Route::get('/invite/{code}', 'MyController#get_my_action');
Route::post('/invite/{code}', 'MyController#post_my_action');
Then in controller named as MyController create a function like
public function get_my_action($code){
//your code goes here
}
or
public function post_my_action(Request $request, $code)
{
//your code goes here
}
With that route:
Route::get('/{page}', 'UserController#show');
In the controller you can do that:
public function show()
{
$page = request()->route('page');
or that:
public function show($page)
or that:
public function show(Request $request, $page)
Try this:
Routes.php
Route::get( '/{page}', 'UserController#show' );
UserController.php
public function show($page) {
dd($page);
}
Inside your controller define your show function as below:
function show(Request $request)
{
echo $request['page'];//or whatever data you want to pass
}
///add follwoing line before you define controller
use Illuminate\Http\Request;
I have controller with show function:
class CssController extends BaseController
{
public function show($id)
{
$csstablepost = CssTable::findOrFail($id);
return View::make('posts/csspost1', compact('csstablepost'));
}
}
And my route:
Route::get('/css3/{id}', 'CssController#show' );
Everything is working fine and corect id is in route, but I want get $title in my route. I have title and id column in database. Why my route isn't working with title if i change all id to title, but works with id? Is there a way, to get my title in route name?
I'm assuming you are changing your query to CssTable::findOrFail($title); and findOrFail() looks for the PK on your table. You'll want to change this too something like CssTable::where('title', '=', $title)->firstOrFail();.
Full Example:
class CssController extends BaseController
{
public function show($title)
{
$csstablepost = CssTable::where('title', '=', $title)->firstOrFail();
return View::make('posts/csspost1', compact('csstablepost'));
}
}
Route::get('/css3/{title}', 'CssController#show');
See more documentation.
Try with following code-
class CssController extends BaseController
{
public function show($id)
{
$csstablepost = CssTable::where('id', '=', $id)->first();
return View::make('posts/csspost1')->with('csstablepost', $csstablepost);
}
}
Now go to route page-
Route::get('css3/{title}', 'CssController#show');
Hope, it will work.