I'm currently studying laravel and for my small project, and I'm having a small issues now.
I'm trying to handle php input in url i.e http://example.com/?page=post&id=1
I currently have this in my controller for post.blade.php
public function post($Request $request)
{
$page = $request->input('page');
$id_rilisan = $request->input('id');
$post = Rilisan::where('id_rilisan', '=', $id_rilisan)->first();
if($post = null)
{
return view('errors.404');
}
return view('html.post')
->with('post', $post);
}
and this is the controller
Route::get('/', 'TestController#index');
Route::get('/{query}', 'TestController#post' );
How to process the php input to be routed to controller? I'm very confused right now, I've tried several other method for the Route::get
This route Route::get('/', 'TestController#index') directs user to the index route. So, if you can't change URL structure and you must use this structure, you should get URL parameters in the index route like this:
public function index()
{
$page = request('page');
$id = request('id');
You can use it a as parameter on your controller :-) see this answer please: https://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller
for example query parameter in route would be $query parameter in controller method :-)
So like this:
Route::get('/{query}', 'TestController#post' );
//controller function
public function controllerfunc($query){}
Why do you need to use query parameters in url. You can simply use this structure http://example.com/posts/1
Then your routes will look like this:
Route::get('/posts/{post}', 'PostsController#show');
And you will be able to access Post model instantly in your show method.
Example:
public function show(Post $post) {
return view('html.post', compact('post'));
}
Look how small is your code now.
Related
This is my url -> http://localhost:82/?search=asd
How do I catch 'asd' in route?
I try this -> Route::get('/?search={SearchValue}', 'TryController#search');
But it didn't work.
It won't even get into the controller.
You don't have to adjust the routes. But include $request in your controller method. Then use your request object to access it.
use Illuminate\Http\Request;
public function search(Request $request) {
// to access the query parameters
$search = $request->query->get('search');
// similar but different syntax
$search = $request->query('search');
// generic method that checks all input including query
$search = $request->input('search');
}
You should just only need something like this:
Route::get('/search', 'TryController#search')->name('try.search');
Once you have the route correctly set you can call:
public function search(Request $request)
{
$request->get('search')
to get the url parameter you passed into the request.
I am using Laravel AdminLTE and I have it all configured, there is just one part I do not understand. I made my route like so:
Route::get('/admin/painlevel', function () {
return view('painlevel');
});
and I have this method in app/Http/Controllers/v1/PainLevelController.php
public function index()
{
return PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
}
How would I call that method and display the data in my painlevel view?
Your current route is merely returning the view('painlevel') directly.
You need to update your route to:
Route::get('/admin/painlevel', 'V1\PainLevelController#index');
In your controller:
public function index()
{
$data = PdTpainlevel::select('pkpainlevel as id', 'painlevel_name as name')->get();
return view('painlevel', compact('data'));
}
You might want to start glancing through the documentations, start with Route, Controller and View
route create like this
Route::get('/administrator', 'administrator\LoginController#index');
and controller create like this
public function index()
{
$data['title']="Admin | DashBoard";
$data['name']="Dilip Singh Shekhawat";
view('administrator/menu_bar',$data);
return view('administrator/dashboard',$data);
}
its working .
I am very obviously a noob to Laravel and hope that someone can help me out.
The about screen is accessed through the route
Route::get('/about', array('as' => 'about', function()
{
return View::make('about')->with('title','About Screen')->with('class','about');
}));
The variables $title and $class are accessible in about.blade.php by {{ $title }} and {{ $class }}. If instead, I have a Controller in between,
Route::get('hello/create', array('as' => 'create', 'uses' =>
'HelloController#create', function()
{
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}));
How do I access $title and $class in the HelloController.php code (so that I can propagate the values to the coming View)?
P.S. I do know about the /hello/create/{name of variable} which is the answer on nearly all questions similar to this, but don't know how to use it to transmit variables NOT keyed onto the Http Request.
$title and $class are the values you are manually giving to the blade. These aren't the values that you are receiving in GET parameters in your route. So, you would do it the same way as you did in the closure.
Your route:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController#create'));
Controller method:
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
UPDATE:
From what I understood, you can also call controller's method inside the route's closure and pass parameters to the controller and call the view with these values inside the controller's method.
Your route file:
use App\Http\Controllers\HelloController;
Route::get('hello/create',function(){
$hello_obj = new HelloController();
return $hello_obj->create('create','hello.create');
});
Controller method:
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
First you need to clear your flow. You are -at the moment- manually setting the variables to be returnet to the view, so your route should look like this:
Route::get('hello/create', 'HelloController#create');
Then, your controller handles the logic:
public function create(Request $request)
{
return view('hello.create')->with('title','Create')->with('class','hello.create');
}
Now, if you need to send parameters from your frontend to your controller, you have two options:
Define route parameters.
Use query params.
Option 1
For the first option, you'll need to define your required/optional parameters in the route itselft:
Route::get('hello/create/{a_variable}', 'HelloController#create');
Then you access this parameter in any of this ways:
public function create(Request $request)
{
return view('hello.create')->with('a_variable', $request->a_variable);
}
or injecting the variable in the method:
public function create(Request $request, $a_variable)
{
return view('hello.create')->with('a_variable', $a_variable);
}
Option 2
For the use of query params, you should include this options when making the request. If your route looks like this:
Route::get('hello/create', 'HelloController#create');
You could specify query params like this:
GET www.my-domain.com/hello/create?first_parameter=value_1&second_parameter=value_2
So in your controller you access this values like this:
public function create(Request $request)
{
$value_1 = $request->get('first_parameter');
$value_2 = $request->get('second_parameter');
return view('hello.create')
->with('value_1', $value_1)
->with('value_2', $value_2);
}
You are alreading sending data to view using with().
Echo it in your view file using $variablename set in with() Example: <?php echo $title; ?>
My Code in route.php:-
Route::get('/register/{id}',array('uses'=>'UserRegistration#id));
I want to call function id (that can be any function of controller) in UserRegistration controller.
Url is like this:- http://localhost:8000/register/test,
http://localhost:8000/register/login
here test and login are function in controller.
{id} is the parameter you're passing to route. So, for your routes go with something like this:
Route::get('/register/id/{id}',array('uses'=>'UserRegistration#id));
//this route requires an id parameter
Route::get('/register/test',['uses'=>'UserRegistration#test]);
Doing this you can call your functions but it is not the recomended way of doing it. Having separete routes foreach function allows for a more in depth control.
public function id(Request $request)
{
return $this->{$request->id}($request);
}
public function test(Request $request)
{
return $request->all();
}
I guess I'm missing something really obvious here but how do you access URL params in Lumen? I have the following route:
$app->get('user/{id}', ['uses' => 'userController#testId']);
Then in my user controller I have:
public function testId(Request $request) {
return $request->input('id');
}
But the ID is always null what have I missed even in this very basic example?
Okay, doesn't matter, as I thought I'm the tool here.
For anyone else (and myself in the future when I land on this page after a Google with the exact same problem next year) the documentation states:
If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies. For example, if your route is defined like so:
$app->put('user/{id}', 'UserController#update');
public function update(Request $request, $id)
{
//
}
So you need to pass any URL params into the function, they don't appear to be accessible via Lumens $request object.
public function testId(Request $request, $id) {
return $id;
}
According to https://laravel.com/docs/master/requests you can do the following:
$app->get('user/{id}', ['uses' => 'userController#testId']);
public function testId(Request $request, $id) {
//$id is from the path
}
The reasoning is that id is part of the request path and not the request input
Try with this
public function testId(Request $request) {
return $request->id;
}