Codeigniter parameters - codeigniter

I've read the URI parameters user guide and still have a question:
http://codeigniter.com/user_guide/general/routing.html
With the following:
{http://myapp/locations/1} I get a 404 error...
{http://myapp/locations} appropriately executes the index() function in the Main controller
{http://myapp/locations/main/locations/1} works, and the value is passed properly to index($var)
I do have other functions in Main.
How is it possible to get the first line to work in order to clean URL's?
Thanks in advance,
Alan

CodeIgniter reads an url as domain/controller_name/method-name/method_parameters and in your first url here http://myapp/locations/1 the first portion (myapp) is your domain name, second (locations) is your controller name and the third portion should be controller's method name and in this case you've passed 1 and obviously there's no such a method name, so it's showing error.
if you pass domain/controller_name like you did here in this url http://myapp/locations, then CodeIgniter reads the first portion as the domain_name and the second portion as controller_name and when there is no third portion in the url then CodeIgniter calls the index method/function by default, so your second url is working.
In your last url you have http://myapp/locations/main/locations/1 and it's been read as
myapp-domain name
locations-controller name
main-method/function name
and rest of all are passed as main controller's arguments. So remember that, the third part of an url is method/function name and if third part is not given then CodeIgniter calls the index method by default and in that case you have to declare a default index method/function in that controller, otherwise an error will be occured.

Related

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

How to custom API URL on codeigniter

I have a PHP CodeIgniter Controller with name index and have a method that get details of id kode ($kode) using API get method.
Now when i need to show kode data for example for id AALI
I call this URL
http://www.example.com/?q=AALI
My target
How to make user data accessible by next URLs
http://www.example.com/AALI
I've try using function _remap on code Igniter, but it still wont work.
Have a look at Codeigniter URLs
As per your statement, your controller name is index and there would be an index function in your controller which renders the default view. it means you have changed default_controller to index in your Config.php
Now if you read the link above about Codeigniter URLs, there is a way to get data which passed in the URL after "/" You have to load url helper you can either autoload(recommended) it or load it in your constructor or Controller as per your convenience
Then you can just type
$param=$this->uri->segment(2); // in case your URL is http://www.example.com/AALI
The first segment is controller itself, The second is the function if your url is complete and the third is the parameter in CI URL structure but if you are not providing function name the first segment will always be your controller . So the second is your parameter. Just save it in a variable and do what you like.

Getting the controller for a form in magento

I have this action defined in my form.phtml (Environment is magento).
form action="getUrl('contacts/index/post')
Now I need to know which controller is getting called so I can get the values in the backend.
That is pretty straight forward.
The first word contacts is the front-name which in magento standards is the module name. The second word index is the controller file name and the last word post is the action of the controller.
Because thats a core controller, you can find the file under /app/code/core/Mage/Contacts/controllers/IndexController.php
Look at the postAction() function.

Make the route parameter actually appear in the address bar

I have a tiny application in MVC 3.
In this tiny application, I want my URLs very clear and consistent.
There's just one controller with one action with one parameter.
If no value is provided (that is, / is requested by the browser), then a form is displayed to collect that single value. If a value is provided, a page is rendered.
The only route is this one:
routes.MapRoute(
"Default",
"{account}",
new { controller = "Main", action = "Index", account = UrlParameter.Optional }
);
This all works fine, but the account parameter never appears in the address line as a part of the URL. I can manually type test.com/some_account and it will work, but other than that, the account goes as a post parameter and therefore does not appear. And if I use FormMethods.Get in my form, I get ?account=whatever appended to the URL, which is also not what I want and which goes against my understanding. My understanding was that the MVC framework would try to use parameters set in the route, and only if not found, it would append them after the ?.
I've tried various flavours of setting the routes -- one route with a default parameter, or one route with a required parameter, or two routes (one with a required parameter and one without parameters); I've tried mixing HttpGet/HttpPost in all possible ways; I've tried using single action method with optional parameter string account = null and using two action methods (one with parameter, one without), but I simply can't get the thing appear in the URL.
I have also consulted the Steven Sanderson's book on MVC 3, but on the screenshots there are no parameters either (a details page for Kayak is displayed, but the URL in the address bar is htpp://localhost:XXXX/).
The only thing that definitely works and does what I want is
return RedirectToAction("Index", new { account = "whatever" });
But in order to do it, I have to first check the raw incoming URL and do not redirect if it already contains an account in it, otherwise it is an infinite loop. This seems way too strange and unnecessary.
What is the correct way to make account always appear as a part of the URL?
My understanding was that the MVC framework would try to use
parameters set in the route, and only if not found, it would append
them after the ?
Your understanding is not correct. ASP.NET MVC doesn't append anything. It's the client browser sending the form submission as defined in the HTML specification:
The method attribute of the FORM element specifies the HTTP method used
to send the form to the processing agent. This attribute may take two
values:
get: With the HTTP "get" method, the form data set is appended to the URI specified by the action attribute (with a question-mark ("?")
as separator) and this new URI is sent to the processing agent.
post: With the HTTP "post" method, the form data set is included in the body of the form and sent to the processing agent.
ASP.NET MVC routes are used to parse an incoming client HTTP request and redispatch it to the corresponding controller actions. They are also used by HTML helpers such as Html.ActionLink or Html.BeginForm to generate correct routes. It's just that for your specific scenario where you need to submit a user entered value as part of the url path (not query string) the HTML specification has nothing to offer you.
So, if you want to fight against the HTML specification you will have to use other tools: javascript. So you could use GET method and subscribe to the submit handler of the form and inside it manipulate the url so the value that was appended after the ? satisfy your requirements.
Don't think of this as ASP.NET MVC and routes and stuff. Think of it as a simple HTML page (which is what the browser sees of course) and start tackling the problem from that side. How would you in a simple HTML page achieve this?

MVC3 razor remote validation - Controller argument is always empty

I can call the controller but the argument (string) is always null.
All the examples I have found name the controller argument the same as the property we are validating remotely, sounds good/easy, but if you look at fiddler what is really being passed in is the name attribute from the input statement. Well that is problematic in that it is a subscripted name something like Person.EMailAddresses[0].Address, well I can't name my controller parameter like that.
So how do I get around this? There must be a way to specify the controllers parameter name in the remote() attribute?
It cannot be done using the default RemoteAttribute. This is a link to an example I posted of a reusable remote validation attribute, where you can specify the name of the controller, action and the name of the variable used to pass the value to the action.

Resources