How to create folder using a controller function on laravel - php

how can i create a folder using a controller function?
(Routes and all are good, but don't know how to make the folder)
I have a simple form for example:
<form method="POST" action="{{ route('admin.projects.store') }}" enctype="multipart/form-data">
<div class="form-group">
<label name="title">Slug:</label>
<input type="text" id="slug" name="slug" placeholder="ejemplo-de-slug" class="form-control form-control-sm">
</div>
</form>
Route in web.php:
Route::post('projects/postUpload', ['uses' => 'AdminController#storeProject', 'as' => 'admin.projects.store']);
I want to put to the folder the same name of the slug.
public function storeProject()
{
return ;
}
Know how to do it?

The best practice is to use Storage:
Storage::makeDirectory($directory);
It will create a new folder on specified disk (local storage, AWS etc).
If you want to create a new folder on the local disk, you can use File facade:
File::makeDirectory('/path/to/directory', 0775);

Related

Laravel route not defined error when it is clearly defined

I am trying to handle a basic form with laravel and am running in to an issue where my POST route isn't being detected and is resulting in a route not defined error in the blade template. My goal is to resolve this error and post the form to the controller, then access the various form fields with the $request param.
This is the error: Route [become-a-customer] not defined.
I appreciate any suggestions on how to resolve this.
Form
<form action="{{ route('become-a-customer') }}" method="post" class="col-md-8 offset-md-2">
<div class="form-row">
<div class="form-group col-md-6">
<label for="first_name">First Name</label>
<input name="last_name" type="email" class="form-control" id="first_name" placeholder="First Name">
</div>
...
</div>
<input type="hidden" name="_token " value="{{ Session::token() }}"/>
<button type="submit" class="btn">SUBMIT</button>
</form>
web.php
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer');
BecomeACustomerController . php
class BecomeACustomerFormController extends Controller
{
public function postBecomeACustomer(Request $request)
{
$firstName = $request['first_name'];
$lastName = $request['last_name'];
...
...
return redirect()->back();
}
}
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
use this command
php artisan optimize
In Your blade Template, You have used the Named route for the form action but, it is not specified in the route file (Web.php).
Change your route file like this
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
OR, you have to change the form action like this
action="{{ url('become-a-customer') }}"
Using the named route is the best practice for a Laravel project.
you can also define as following where "as" key is for naming your route
Route::post('/become-a-customer', ['uses' => 'BecomeACustomerFormController#postBecomeACustomer', 'as' => 'become-a-customer']);
Check your Apache or Nginx configurations. Sometimes a redirect from https to http will alter the method from POST to GET.
I'd recommend setting up a temporary endpoint for GET by the same Route and placing a dd() statement in it to test the theory.
route() method uses route name which is undefined. You can define it via name() method on route as below
Route::post('/become-a-customer', 'BecomeACustomerFormController#postBecomeACustomer')->name('become-a-customer');
for more see doucmentation
For me url('routeName') worked instead of route('routeName')

Can not get my data from my form in Laravel?

I am new to Laravel, I am trying to have a simple example, but I am getting a 419 error, I dont know why it shows up but I will expalin what I did,
I created a simple Controller and I called it FormController with the command line :
php artisan make:controller --resource FormController
In my web.php I added this :
Route::resource('form','FormController');
my view has a simple form in it :
<form action="/form" method="POST" >
<input type="text" name="cih">
<input type="submit">
</form>
I open my view with the create method :
public function create()
{
return view('contact');
}
I want that when I submit my form I get my data, so I use my 'store' method :
public function store(Request $request)
{
return $request->all();
}
But instead of getting it, I get 419 message, and my session has expired etc ..
I followed a course and that what the teacher was doing I believe nothing more, so I would appreciate any help, I need it.
Thank you
You need to include the CSRF token while submitting a form since the 'VerifyCsrfToken' middleware is enabled by default for the web routes in App/Http/Kernal.php.
<form action="/form" method="POST" >
#csrf
<input type="text" name="cih">
<input type="submit">
</form>
And, Welcome to Laravel!

laravel 5.2 Authentication Failure

Hello Guys i am new to laravel. When the new user register with authentication i am this error message. How to resolve my problem
The Authentication Failure with this error message.
Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\customer given.
My controller is
public function store(Request $request)
{
$user = new customer;
$user->name=Input::get('name');
$user->email=Input::get('email');
$user->password=Input::get('password');
$user->save();
Auth::login($user);
return redirect::home();
}
and my routes
Route::get('register', 'testing#index');
Route::post('store', 'testing#store');
Route::get('login', 'testing#create');
Route::post('logout', 'testing#destroy');
and my register page is
<form action="store" method="post">
<label for="name">Name</label>
<input type="text" name="name" autocomplete="off">
<br>
<label for="email">Email</label>
<input type="text" name="email" autocomplete="off">
<br>
<label for="password">Password</label>
<input type="text" name="password" autocomplete="off">
<br>
<input type="hidden" name="_token" value="{{csrf_token()}}">
<br>
<input type="submit" name="submit" value="Submit">
</form>
Please help me guys how to register,login and logout with complete authentation.
Thanks you and welcome your suggestions.
Without knowing your actual model of customer I try to give you the best matching answer.
If you are very new to laravel the best appraoch ist to use
php artisan make:auth
To create:
Home View
App Layout
Auth Routes
Auth Controller
Password Reset
This will be done for you, using the default, already existing, App\User model. If you DO NOT want to user the default App\User and you want to use your App\Customer for authentication, then you will need to at least make your model extend Authenticable
use Illuminate\Foundation\Auth\User as Authenticatable;
class Customer extends Authenticatable
{
...
}
Without this you will surely receive the given error. However, you will still need to ensure that there are the required fields on your Customer model - like email, password, remember token etc. If you do not want to use these you will need to make further adaptions within your authentication controller.
As mentioned the best approach for beginners is using the generated auth. You can find more here: https://laravel.com/docs/5.2/authentication
Be careful - php artisan make:auth will create a few views and it will overwrite existing ones with the same name.
e.g.
/resources/views/home.blade.php
/resources/views/layout/app.blade.php
/resources/views/auth/login.blade.php
/resources/views/auth/register.blade.php
and a few more within auth
Looking at your code you could simply try replacing
$user = new customer;
with
$user = new User
and ofcourse pull in the user model
use App\User;

Forms in Symfony2

I'm trying to create a simple search form in Symfony2.
This is my form:
<form action="/search" method="GET">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search ...">
<span class="input-group-btn">
<button class="btn-u btn-u-lg" type="button"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
So my question is, what is the syntax of the form action? Do I just enter in the exact file that receives it? Or do I need to call some kind of config file?
Another question is how do I handle the search in the controller?
Thanks in advance!
Please note that I'm a total noob in Symfony2 :)
EDIT:
How do I handle the request if I would like a nice url like this: ".../search/value" instead of ".../search?q=value"?
Here is my action:
/**
* #Route("/search/{value}", name="search")
*/
public function searchAction($value)
{
}
in your action you need to put the logical path of your controller to do so call the twig function {{path('you route alias')}} , I assume that you have already set your route configuration.
to handle search in controller that's will depend on you re own logic but in the general case you will have to get the searched word using the request object taht should be some think like that:
public function searchAction(Request $request){
$objet=$request->query->get("word");
//do staff
return $this->render('Your Bundle:views:searchResult.html.twig')
}
In the form action, you need to enter the route that points to your controller. For example if you have a route that is set up to point to "/search" which uses your searchController's search method, you'd have to write action="/search" so when a user submits this form it is going to point to your controller's method.
In your controller you can either grab the $_GET variable with the name of the input (by the way you need to add a name for your input field to be accessible via the request superglobals), or pass the value directly in your url and put an optional variable after your "/search".

PHP Route not defined laravel

Working on a job portal, so I arrived at a point where employers need to edit their posted jobs,On page load it gave me an error Route [employers/job/save/Mw==] not defined, please I need help my deadline is 3hours from now!
Here is my code:
Routes:
//Route for Employer's specified Job Editting -> To get ID as argv
Route::get('employers/job/edit/{id}', 'employerController#editJob');
//Route for Employer's to save specified Job after Editting -> To get ID as argv
Route::post('/employers/job/save/{id}', [
'as' => 'saveJob',
'uses' => 'employerController#saveJob'
]);
View:
{{ Form::open(['action'=>'employers/job/save/'.base64_encode($jobData->id),'class'=>'full-job-form', 'id'=>'jobForm','role'=>'form']) }}
<div class="form-group col-lg-12 col-sm-12 col-md-12">
<label class="sr-only" for="">Job Title</label>
<input type="text" class="form-control"
name="job_title" placeholder="Job Title"
value="{{ $jobData->job_title }}">
<span class="help-block">Eg. Marketing Manager</span>
</div>
Your issue is that you're using the action parameter for your Form::open() call. This expects the name for a controller method (e.g. {{ Form::open(['action' => 'employerController#saveJob']) }}. If you want to link to a pre-generated URL use the url parameter:
{{ Form::open(['url' => 'employers/job/save/'.base64_encode($jobData->id)]) }}
That said, that's not the best practice, as, if you change your routing system, you now have to change all these hardcoded URLs. As such, you should rely on named routing or controller actions.
Now, your route is already named ('as' => 'saveJob') so you should actually use the route parameter of Form::open():
{{ Form::open(['route' => ['saveJob', base64_encode($jobData->id)]]) }}
Alternatively, you could use the action parameter as you are currently trying to do (albeit erroneously):
{{ Form::open(['action' => ['employerController#saveJob', base64_encode($jobData->id)]]) }}
See the docs on forms for more information.
Also, as #TheShiftExchange says, its a bit odd to be using the base 64 encoded id, why not just use the raw id?

Categories