create a form action with html in yii - php

I have the following from in yii.
<form action="" method="POST">
I would like to to the following:
<form action="/protected/plans.php" method="POST">
However, that doesn't work. What is the proper way for providing the link in the form action in yii. Thank you for the help.

You can't access from url to files in /protected folder. It's private forlder to use only on server side. Move you file plans.php to project's root directory or create another folder near /protected.

As SiZE said, You can't access the files inside protected, because yii projects have a .htaccess file in the root and this file has deny form all line. First way is to move file out of protected folder. But I think it's not a good choice at all. You are using an MVC Framework and you should use this structure in your project. For example you should use MVC url pattern, not default pattern. If you want to use plain url pattern, Why do you use mvc framework?!
I recommend you to learn more about mvc architecture and yii framework.

In order to use Yii effectively, all requests from your web page should go through the controller mechanism build into Yii.
You can read more about this here.
http://www.yiiframework.com/doc/guide/1.1/en/basics.controller
You will have a create a controller, which consists of one or more actions. Your web form will then send a request to invoke this action.
As an example, so you have a controller Plans that supports an action create
// In file protected/controllers/PlanController.php
class SiteController extends CController
{
public function actionCreate()
{
// Put your code here to create a new plan
echo "I am going to create your plan";
some_other_code();
Yii:app()->end();
}
public function actionShowform()
{
$this->render("form");
}
}
You can then create a url to access the action.
// In file protected/views/plan/form.php
<form action="<?php echo Yii::app()->createUrl("plan/create"); ?>" method="POST">
<!-- form details here -->
</form>

Related

How to connect codeigniter controller method from Jquery code in a view file (placed in folder)

I am using the autocomplete plugin in Jquery as follows:
$(function(){
$("#username").autocomplete({
source: "http://localhost/websitename/index.php/admin/suggest_names" // path to the method in controller
});
});
The code for the text box is
<input type="text" name="username" id="username">
The suggest_names method in controller in turn calls a method in the model
I have tried providing the path to the method in controller by using
source: "admin/suggest_names"
in the above code.
However this is not working when the file is placed in a sub folder within the views folder application/views/admin. This works when the file is placed directly within the views folder.
I have tried using site_url but this does not work as well. (the form helper function is loaded in the administrator)
Can you suggest where I am making a mistake and suggest the syntax for the code to be used.
Stick to the following structure:
VIEW:
Your jquery function must be in a view file (it can be in the
Application/Views
directory or in a subdirectory of view). A view file is displayed in the browser and so the jquery code can run on client side. The controller is called from here. Your first code block example seems to be correct. You can also use: source: "/index.php/admin/suggest_names".
CONTROLLER:
A Controller (in your case called admin) is simply a class file that is named in a way that can be associated with a URI. They are placed in the
Application/Controllers
directory or a subdirectory of it. In your case you are calling the function suggest_names(), where you prepare the $data you want to output in the view file and call the database functions from a model (that last one is optional, you could treat the database functions from the controller as well).
MODELS:
Models are PHP classes that are designed to work with information in your database. They are placed in the
Application/Models
directory or subdirectory of it.

Define base url for laravel

I am new to laravel. My url for my only controller is
http://localhost/myapp/public/users
In the views for the form functions, I don't want to have to do an absolute path to the post function, e.g.
<form method='post' action='/myapp/public/user/update-user'>
I want to just set the users controller as the base url path so I can say in my form
<form method='post' action='/update-user'>
Is this possible?
Use URL::to()
or simply use laravel form
{{Form::open(array('url'=>'route_name'))}}
You can use URL::to() function.Laravel supports RESTful API or RESTful routing. So it is not a good idea to use routing like this.So use like,
In app/routes.php
// for view (GET)
Route::get('users', function(){
View::make('users');
});
Route::post('users', function(){
// do form validation and process here or call a function from controller
});
Then in your form use,
echo Form::open();
// form elements
echo Form::close();
Yes, it is possible, but it's a little complicated because laravel isn't really set up to support this.
You need to change the file vendor/laravel/framework/src/Illuminate/Http/Request.php, replacing the function root() with this:
public function root($domain = NULL)
{
return rtrim(($domain?:$this->getSchemeAndHttpHost()).$this->getBaseUrl(), '/');
}
And then in the getRootUrl($scheme, $root = null) method in the file vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php you need to change this line:
$root = $root ?: $this->request->root();
to this:
$root = $this->request->root($root);
That should allow laravel to automatically detect and handle subfolders correctly, so you will be able to use the standard URL methods.
First, you need to hide the /public/ part of your URL using the virtual host or the alias in your web server (apache or ...). The entry point (http://some.host/alias/ or http://some.virtual.host/) should lead directly to the public folder (i.e. /whatever/path/myapp/public/).
Second, using the routes, you can do the remaining easily. The route
Route::get('update_users', "UsersController#updateAction")
will redirect the http://whatever.host/update_users to the correct action without the need of the ugly format.

Not able to add a link in code igniter

I am a beginner to Wamp server. I am trying to design a website with project name as "helloall" in netbeans IDE.
In the views folder I have two files layout1.php and layout2.php.
I am trying to call layout2.php from layout1.php in the below style.
<div id="logo"> <span>LAYOUT2</span> </div>
But I am facing the below error, for which I am not able to find the reason.
The requested URL /helloall/layout2.php was not found on this server.
Do I need to change anything in the configuration? I am using all the default configurations.
as per Codeigniter standard you have to follow the MVC pattern so:
Model -> Controller ->view
now, assuming you want to visualize layout2.php view you have 2 chances:
1 - load view directly where you need $this->load->view('layout2');
2 - create url function ad hoc kind of www.site.com/layout/layout1 and www.site.com/layout/layout2:
controller layout.php
class Layout extends CI_Controller {
function layout1(){
$this->load->view('layout1');
}
function layout2(){
$this->load->view('layout2');
}
}
i really suggest you to look at How To Create a Controller in Codeigniter Doc

How to start a new website project in codeigniter?

I'm really beginner to codeigniter I'm working on CI since last 2 weeks. During this period I have created many views.php files, some controllers.php files and some models.php files
Now I want start a new website project.
What should I do. Should I delete all files of my controllers, views and models, etc., and download another codeigniter and start from the beginning?
You should check the documentation of codeigniter for help but just to give you a quick start ill explain how to create your first codeigniter project.
Installation
1 Download the codeigniter framework from http://ellislab.com/codeigniter
2 upload it in root directory of your website or local apache server directory.
Creating your codeigniter project.
In codeigniter your controller will handle the url requests and load appropriate model and views. So the first step is to create your controller.
1 Creating your controller: go to Applications->controllers and there you will find a built in controller called welcome.php.
This controller loads a view welcome_message.php which is inside Application->views.
You can use this controller or create your own.
To create your own controller create a new php file myfirstcontroller.php and extend a class with same name from CI_Controller.
Note that the name of the file and your class name should be the same. the index function is the default function that will be called when you make a request to the controller
class myfirstcontroller extends CI_Controller {
public function index(){
$this->load->view("myfirstview");
}
}
so when you request this controller through yoursite/index.php/myfirstcontroller
it will load a view called myfirstview.php which will reside inside applications->views.
Go ahead and create this file in applications ->views.
2 To pass data from controller to view you will send an array to the view
class myfirstcontroller extends CI_Controller {
public function index(){
$data['name']="My first application.";
$this->load->view("myfirstview",$data);
}
}
3 You can access this variable in view
echo $name
and it will output your variable
3 you use models you have to create a file inside applications->models and call it from controller and it will return the result in the form of array.
You can look at the documentation for further help.
Hope this helped you to get started with codeigniter.
The user guide is inside your download library.
You can also view it in http://ellislab.com/codeigniter/user-guide/
Good luck!!!
Here is Phil Sturgeon's article on how to do multiple site on one CI instance, in here he explains 2 ways of doing it and describes pros and cons.
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
But in his latest articles he has told what happened to modular separation.
http://philsturgeon.co.uk/blog/2010/03/modular-separation-codeigniter-2

CakePHP - REST POST functionality not working with default mapping

I am new to CakePHP and am attempting to configure my web app to use REST.
I am using CakePHP version 2.0.4.
Following the CakePHP Cookbook, I have entered the following into the routes.php file ('Apples' pointing to the ApplesController.php controller):
Router::mapResources('Apples');
Router::parseExtensions();
and in the controller itself (ApplesController.php), I have added the following component:
public $components = array('RequestHandler');
Also in the controller, I have an add action which echo's a string (as follows):
function add() {
echo 'The add action has been called';
}
Finally, I created a view (index.ctp located in app/View/apples/) which uses a HTML form with the method POST and the action "/apples/" to submit.
<h2>Home<h2>
<form action="/apples/" method="post">
<input type="submit" value="Submit" />
</form>
Now according to the the CakePHP Bakery (http://bakery.cakephp.org/articles/rightwayindia/2010/01/11/restful-web-application-development-in-cakephp), the mapResources should automatically map POST to the 'add' action of the controller specified in the routes.php file, therefore when the button is clicked, should echo the string in the add action.
This only works when you change the action parameter in the HTML (in index.ctp) to:
<form action="/apples/add" method="post">
and explicitly point to the add action.
I may be wrong, but I thought that by configuring REST in the routes.php file should automatically map the specific REST methods to the actions such as add, edit, delete etc.. (stated on the web page linked above) to the controller stated in the paramater of the mapResources function.
I have also tried custom REST routing but this is also not working. However, It would be nice for it to work with default mapping rather than customising it.
Any suggestions would be appreciated. Thanks.

Categories