How to set variable in routes - laravel - php

I have the following route in my routes.web.php. Looks like this...
Route::get('/spielerAuswahl/{somevar}', 'SpielplanController#getHeimGast');
In the variable {somevar} I have for example Nr1=111&Nr2=222. The route works fine in a get to SpielplanController...
public function getHeimGast(){
$var = $somevar;
return view('test')->with('variableControllerSomevar', $var);
}
In this function I want to put the Nr1=111&Nr2=222 in the $var and make then an easy output of this variable in the view. How to get that?

Though the answer come late, but you can put Nr1=111&Nr2=222 as uri with proper encoding. For javascript, encodeURIComponent can be used to escape certain characters to form a valid URI:
encodeURIComponent('Nr1=111&Nr2=222');
// output: Nr1%3D111%26Nr2%3D222
To request server, using this url:
http://example.com/spielerAuswahl/Nr1%3D111%26Nr2%3D222
The controller function then receives correct form of variable as follows:
public function getHeimGast ($somevar) {
$somevar // = Nr1=111&Nr2=222
}

Only URI segments can be map as route variables in Laravel, no query string variables.
You can simply fetch those like these:
public function getHeimGast(){
$vars = request()->all();
return view('test', $vars);
}
In your test blade, you can use those query vars, eg( Nr1=111&Nr2=222 ) as:
$Nr1, $Nr2 ... and so on...
NOTE: given a query like this: /spielerAuswahl?Nr1=111&Nr2=222

Related

Try to double filter sql query data laravel

guys i trying to filter my data from mysql with where clause but after put secound value laravel give me a blank result? If i try to filtered with first value example like this : http://localhost/transport/1 everything is good but if i try to set from destionation give me a blank result. example with fail : http://localhost/transport/1/Германия
Here is my Controller
class TransportController extends Controller
{
public function filtermethod($method){
$data['ads'] = db::table('ads')->where('method', $method)->get();
return view('transport', $data );
}
public function regionfrom($from){
$data['ads'] = db::table('ads')->where('from', $from)->get();
return view('transport', $data );
}
Here is my routes :
Route::get('transport/{method}', 'TransportController#filtermethod');
Route::get('transport/{method}/{from}', 'TransportController#regionfrom');
Your second route should be giving your controller 2 variables.
public function regionfrom($method, $from)
Is what your route your having problems with is calling, do the logic you like in there.
If you would like to filter twice, try this:
$data = DB::table('ads')-where('method', $method)->where('region', $region)->get();

fetching parameter in a link php codeigniter

Say I have this url
http://localhost/newtkt/index.php/welcome/gettkt/PfIfiETYXzUpYRJf6RPvyncN4PgdN3
and a controller function
public function gettkt()
{
//mydata
}
how can i retrieve the parameter PfIfiETYXzUpYRJf6RPvyncN4PgdN3 that comes with the url and make it a variable in my controller function gettkt() so that i can use the parameter within the function like gettkt(thisparameter). I hope I've structured my question in the best way possible. thank you.
You can do two things
Method 01
public function gettkt($parm)
{
echo $parm; # Prints PfIfiETYXzUpYRJf6RPvyncN4PgdN3
}
Method 02
Use URI Class in CodeIgniter
$parm = $this->uri->segment(n); # Number point your parameter
$parm = $this->uri->segment(3); # Prints PfIfiETYXzUpYRJf6RPvyncN4PgdN3
You can use
$this->uri->segment('3'); //where 3 is for 3rd param
for more information visit URI

laravel controller function parameters

I'm trying to call a function inside one of my controller from the action() helper function. I need to pass a paramter to my function.
Here is the funciton I'm trying to call :
public function my_function($clef = 'title')
{
$songs = Song::orderBy($clef)->get();
return View::make('my_view', compact('songs'));
}
Here is the way I call it :
Author
The function is always running with the default value, even if I put anything else in my call. From what I can see in my address bar, the paramter seems to be sent along with the call :
http://www.example.com/my_view?clef=author
From the little I know, it seems correct to me, but since it doesn't work, I must come to the evidence that it isn't. What would be the cleanest way to call my function with the right parameter?
The reason why it's not working is because query strings aren't passed as arguments to your controller method. Instead, you need to grab them from the request like this:
public function my_function(Request $request)
{
$songs = Song::orderBy($request->query('clef'))->get();
return View::make('my_view', compact('songs'));
}
Extra tidbit: Because Laravel uses magic methods, you can actually grab the query parameter by just doing $request->clef.
Laravel URL Parameters
I think assigning parameters need not be in key value pair. I got it to work without names.
If your route is like /post/{param} you can pass parameter as shown below. Your URL will replace as /post/100
URL::action('PostsController#show', ['100'])
For multiple parameters say /post/{param1}/attachment/{param2} parameter can be passed as shown below. Similarly your url will be replaced as /post/100/attachment/10
URL::action('PostsController#show', ['100', '10'])
Here show is a method in PostsController
In PostsController
public function show($param1 = false, $param2 = false)
{
$returnData = Post::where(['column1' => $param1, 'column2' => $param2 ])->get();
return View::make('posts.show', compact('returnData'));
}
In View
Read More
In Routes
Route::get('/post/{param1}/attachment/{param2}', [ 'as' => 'show', 'uses' => 'PostsController#show' ] );
URL Should be: http://www.example.com/post/100/attachment/10
Hope this is helpful.

Accessing variable from helpers file in views - Laravel 4.2

I have made a helpers file in my /app folder which contains the following:
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
echo $C['business_name'];
This works, but if I try
echo $C['business_name'];
In one of my views I get an error of $C undefined. I have added the helpers file to my start/global file and I know it works...
What steps should I take to use this variable in my views?
You need to pass data directly into the view via the second parameter of View::make or alternatively View::make('someBlade')->with(data);
So in your case it might be something like:
View::make('someBlade', $C);
If you really, really want globals, you can do this for views:
View::share('c', $C);
http://laravel.com/docs/4.2/responses
I think you need to create a function in helper and call that function in view.
it will automatically display value of this variable but you need change "echo" replacing with "return" in function last line.
function xyz()
{
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
return $C['business_name'];
}
Call this function in your view like this. {{xyz()}}
if you are returning array {? $abc=xyz(); ?} make blade filter not echoing value pass this function to array variable and show like this {{$abc['business_name']}}

CodeIgniter Restserver doesn't work with specific URL's

I am using this Restserver in combination with CodeIgniter.
It seems to work pretty well except when I use URL's like these;
mydomain.com/api/example/1234/
wherein 1234 is the ID I'm requesting.
Code like this doesn't seem to work:
class Example extends REST_Controller {
public function index_get() {
print($this->get("example"));
}
}
It doesn't seem to matter whether it is a GET or POST request. There must be a way I can just retrieve the ID from the URL..
The URL's segments should equal to these:
api = Controller
example = Resource
Parameters must be in key-value pairs:
id = Key
1234 = Value
It seems that your 1234 is treaded as a key, but with no value for it. Try to change your URL to the following: mydomain.com/api/example/id/1234/, which would translate to: mydomain.com/controller/resource/key/value/
There is also a very detailed tutorial here: http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
EDIT:
Since your controller is in a sub-folder, your URL's segments should be constructed like this:
api/example = Controller
user = Resource // basically the method name + the http method e.g. user_get(), or user_post(). I just made 'user' up, for your app can be whatever it is that people will access via your api
Parameters must be provided as key-value pairs:
id = Key
1234 = Value
So then your URL would look like this: mydomain.com/api/example/user/id/1234/
in the case of a GET REQUEST you just define the parameters in the header of the function like this:
public function index_get($param) {
print($param);
}
if you want to make it optional then:
public function index_get($param="") {
if ($param=="") {
print("No Parameters!");
} else {
print($param);
}
}
If I want to send "POST" parameters, I just create a POST METHOD and receive them as such...
public function index_post() {
$params = $this->post();
if (isset($param['id'])) {
print($param['id']);
} else {
print("No Parameters!");
}
}

Categories