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; ?>
Related
All my requests are starting with a prefix, so I created a Route Group with multiple endpoints:
routes/web.php
Route::group(array('prefix' => $prefix), function() {
Route::get("/test/test2/{lang}", ['uses' => 'TestController#test2']);
...
});
Controller:
class TestController {
public function test2(Request $request, $lang) {}
}
With the following test URL:
domain.com/customprefix/test/test2/en
I reach my controller and can access $lang (=en). But how can I pass $prefix to my controller methods? (It should evaluate to "customprefix" in this example)
Unfortunately I didn't find information about that in the documentation or in the API specification.
In your Controller you can get prefixes as one of these solutions:
1.With $reques:
public function TestController(\Illuminate\Http\Request $request)
{
$request->route()->getPrefix();
}
2.Without $request:
$this->getRouter()->getCurrentRoute()->getPrefix()
Did you try to use route prefix already ? . If not, so it should look like this
public function test2(Request $request, $lang){
dd($request->route()->getPrefix());
}
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();
}
how can i use same route for two different controller function methods in laravel
first controller
public function index()
{
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
second controller
public function index()
{
return view('search.index');
}
i want to use these two different controller functions for one route.
This is my route name
Route::get('/computer', [
'uses' => 'ComputerProductsController#index',
'as' => 'computer.list'
]);
laravel needs somehow to identify which exactly method you want. for example you can pass the parameter, which will identify which method to call.
public function index(Request $request)
{
// if param exists, call function from another controller
if($request->has('callAnotherMethod')){
return app('App\Http\Controllers\yourControllerHere')->index();
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
You can't. If you want to add search functionality to your first controller's index page, you should determine which page to show inside your controller.
A possible example controller:
public function index(Illuminate\Http\Request $request)
{
// If the URL contains a 'search' parameter
// (eg. /computer?search=intel)
if ($request->has('search')) {
// Do some searching here and
// show the search results page
return view('search.index');
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
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.
So I have a route that looks like this:
Route::any('some/page', ['as' => 'some-page', 'uses' => 'SomePageController#index']);
However, I also have ajax calls at the same URL (using a request parameter called ajax like: some/page/?ajax=my_action) that I want to hit methods on my controller:
index already routes: 'SomePageController#index'
ajax = my_action needs to route: 'SomePageController#ajaxMyAction'
ajax = my_other_action needs to route: 'SomePageController#ajaxMyOtherAction'
ajax = blah_blah needs to route: 'SomePageController#ajaxBlahBlah
...
What's the elegant solution to setting this up in my routes.php file?
After inspection of Laravel's Http Request and Route classes, I found the route() and setAction() methods could be useful.
So I created a middleware to handle this:
<?php namespace App\Http\Middleware;
class Ajax {
public function handle($request, Closure $next)
{
// Looks for the value of request parameter called "ajax"
// to determine controller's method call
if ($request->ajax()) {
$routeAction = $request->route()->getAction();
$ajaxValue = studly_case($request->input("ajax"));
$routeAction['uses'] = str_replace("#index", "#ajax".$ajaxValue, $routeAction['uses']);
$routeAction['controller'] = str_replace("#index", "#ajax".$ajaxValue, $routeAction['controller']);
$request->route()->setAction($routeAction);
}
return $next($request);
}
}
Now my route looks like:
Route::any('some/page/', ['as' => 'some-page', 'middleware'=>'ajax', 'uses' => 'SomePageController#index']);
And correctly hits my controller methods (without disturbing Laravel's normal flow):
<?php namespace App\Http\Controllers;
class SomePageController extends Controller {
public function index()
{
return view('some.page.index');
}
public function ajaxMyAction(Requests\SomeFormRequest $request){
die('Do my action here!');
}
public function ajaxMyOtherAction(Requests\SomeFormRequest $request){
die('Do my other action here!');
}
...
I think this is a fairly clean solution.
You can't make this dispatch in the routing layer if you keep the same URL. You have two options :
Use different routes for your AJAX calls. For example, you can prefix all your ajax calls by /api. This is a common way :
Route::group(['prefix' => 'api'], function()
{
Route::get('items', function()
{
//
});
});
If the only different thing is your response format. You can use a condition in your controller. Laravel provides methods for that, for example :
public function index()
{
$items = ...;
if (Request::ajax()) {
return Response::json($items);
} else {
return View::make('items.index');
}
}
You can read this http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_ajax and this http://laravel.com/docs/5.0/routing#route-groups if you want more details.