Laravel 4: (routing?) to search keyword url - json

Let's say for example in results q='sports', how would I be able to make it www.example.com/results?q=sports or whatever? And when I type manually in the url www.example.com/results?q=books it would search for books.
The response is in json btw.

You will need to process the parameter from within whatever controller '/results' is routed to, eg:
if isset($_GET['q']) {
$searchCategory = $_GET['q'];
}
Another option would be to have q as a function parameter on a controller method:
class MyController extends BaseController {
public function __construct() {}
public function getSearch($searchCategory) {
// Do Stuff
}
}
And this would be accessible via:
http://www.example.com/search/books

Related

Passing a variable data in all controllers in codeigniter

Iam developing a website using codeigniter.I want to pass a particular data to all the controller.I searched and find it can be done by session like below.
$var = $this->session->userdata('passingdata');
But this data loses when history is cleared and when session expires.
Is there any other way to pass variable to all controllers.
You can try this
- create MY_Controller in application/core. it looks like
Class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('HomeModel');
$this->global_data['settings']= $this->HomeModel->getSettings();
}
function display_view($view, $local_data = array()) {
$data = array_merge($this->global_data, $local_data);
return $this->load->view($view, $data);
}}
then extend this controller in your all controller
your functions in your controller like
Class Home extends MY_Controller {
public function index() {
$data['title'] = 'test';
$this->display_view('home', $data);
}
}

Yii2 console command pass arguments with name

I want to use commands as
php yii sync anyvar2=anValue anyVar1=anyValue
In controller
public function actionIndex(){
echo $anyVar1;
echo $anyVar2;
}
I tried with php yii sync [--anyvar2=anValue ,--anyVar1=anyValue]
1) If you want to set controller parameters:
class SyncController extends \yii\console\Controller
{
public $anyVar1;
public $anyVar2;
public function options($actionID)
{
return array_merge(parent::options($actionID), [
'anyVar1', 'anyVar2'
]);
}
}
Now you can set them like that:
php yii sync --anyVar1=aaa --anyVar2=bbb
2) If you want to just pass variables as arguments:
public function actionIndex($anyVar1, $anyVar2)
{
// ...
}
Now you can set them like that:
php yii sync aaa bbb
Got solution
when need to pass variable in console
Variable should declared in public scope.
Variable should returned in options function
Ex:
class SyncController extends \yii\console\Controller
{
public $anyVar1;
public $anyVar2;
public function options()
{
return ['anyVar1','anyVar2'];
}
public function actionIndex(){
echo $this->anyVar1."\n";
echo $this->anyVar2."\n";
}
}
In console
php yii sync --anyVar2=1111 --anyVar1=999

Extending Illuminate\Http\Request clears Accept header

I've extended the Illuminate\Http\Request class and am passing it along to my controller.
In my controller, I check if the request has an Accept: application/json header, using the $request->wantsJson() method.
If I use the base Illuminate\Http\Request class it works perfectly fine, but if I use my extended class, it says that the Accept header is null.
use Illuminate\Http\Request;
class MyRequest extends Request
{
...
}
Controller
class MyController
{
public function search(MyRequest $request) {
if ($request->wantsJson()) {
// return json
}
// return view
}
}
This does not work. If I instead replace MyRequest with an instance of Illuminate\Http\Request it works. If I var_dump $request->header('Accept'), it's NULL when using MyRequest.
Extend Illuminate\Foundation\Http\FormRequest instead:
use Illuminate\Foundation\Http\FormRequest;
class MyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
];
}
}
The FormRequestServiceProvider performs a series of configuration steps that set up the request. You could replicate that in your own service provider, of course.

How to define an object in actionscript 3 using a custom class

Hi my problem is i have to be able to reference certain fields inside my Customer object.]
I am studying AS3 at the moment and being taught custom classes, but we are taught to use the toString method of returning a value i guess you could call it, what i need is to be able to call one field to identify the object i.e. name field from the object in the array, here's my code
package valueObjects
{
public class Person
{
//instance variables
protected var name:String;
protected var address:String;
protected var phoneNo:String;
public function Person(n:String,a:String,p:String)
{
name=n;
address=a;
phoneNo=p;
}
public function toString():String
{
//returns string
return name+":"+address+":"+phoneNo;
}
}
}
some reason it will not put that whole block of code together like THIS IS
So now how do i define it not toString but in object form ??
I think what you are trying to do is access the name, address and phoneNo vars from a different class?
If so, you have to declare them as public vars instead of private vars.
public var name:String; //now this can be accessed from other classes: thisClassInstance.name
If you want to have them read-only from other classes, you have to use a getter method:
protected var name_:String; //local var name for full access;
public function get name():String {
return name_; //this can be access by doing thisClassInstance.name
}

AIR dispatch event with parameter

I have a form with a search field.
When user press enter key, I use httpservice to send a query to mySQL database.
In some case (a lot) there are several record, so a new window is opening to show those record with a datagrid to let user chose the good result.
My problem is how to send selected information to the first window (with text field).
I gess that dispatch event is the way but I don't found how to use!
Can you help me to find a solution.
Thanks
If you are trying to communicate within an MDI environment I suggest that you use some kind of shared model ( aka Mediator or Presentation Model ) that keeps a contract between the desired windows.
class SelectionPM{
[Bindable]
public var selectedItem:Object;
}
Use case:
Window1 has an instance of SelectionPM, when you open Window2 you pass
SelectionPM instance to it, then update SelectionPM.selectedItem
property on changing selection in the Window2 datagrid. That will
propagate the binding chain up to Window1, where you can use the
SelectionPM.selectedItem as you wish.
Ideally you would use an IoC container for model injection but that is another story.
That might seem like a lot of work but if you keep this methodology across your app you will benefit from it.
Cheers!
Here's a set of four classes as a basis. Obviously you don't want to be doing the actual work in the constructors as below.
public class App
{
public static var eventDispatcher:EventDispatcher = new EventDispatcher();
public function App()
{
new Window1();
}
}
class AppEvent extends Event
{
public static const DATA_READY:String = "APPEVENT.DATA_READY";
public var data:Object;
public function AppEvent( type:String, data:Object )
{
super( type );
this.data = data;
}
}
class Window1
{
public function Window1()
{
App.eventDispatcher.addEventListener( AppEvent.DATA_READY, onDataReady );
...DO STUFF...
new Window2();
}
private function onDataReady( evt:AppEvent ) : void
{
...DO STUFF WITH "evt.data"....
}
}
class Window2
{
public function Window2()
{
...GET DATA FROM SERVER AND PUT IT IN "data"...
App.eventDispatcher.dispatchEvent( new AppEvent( AppEvent.DATA_READY, data ) );
}
}
</pre>